<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/38">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Hadoop/1. Hadoop 大数据/">Hadoop 大数据</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Hadoop/">Hadoop</a>
			</span>
		
	</div>

	

	
		<h2 id="1-大数据介绍"><a href="#1-大数据介绍" class="headerlink" title="1.大数据介绍"></a>1.大数据介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大数据指的是所涉及的数据量规模巨大到无法通过人工，在合理时间内达到截取、管理、处理、并整理成为人类所能解读的形式的信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大数据，可帮助我们察觉商业趋势、判定研究质量、避免疾病扩散、打击犯罪或测定即时交通路况等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;麦肯锡全球研究院（MGI）预测，到 2020年，全球数据使用量预计将达到 35ZB（1ZB=1000EB，1EB=1000PB，1PB=1000TB，1TB=1000GB）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Google每天要处理的数据高达几百PB。百度每天处理数据几十PB。腾讯微信活跃用户数达7亿，每天产生的数据量上百TB，2016年除夕当日，微信红包的参与人数达到4.2亿人，收发总量达80.8亿个。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;多源异构：描述同一主题的数据由不同的用户、不同的网站产生。网络数据有多种不同的呈现形式，如音视频、图片、文本等，导致网络数据格式上的异构性。</p>
<ul>
<li><p>交互性：不同于测量和传感获取的大规模科学数据，微博等社交网络兴起导至大量网络数据具有很强的交互性。</p>
</li>
<li><p>时效性：在网络平台上，每时每刻都有大量新的网络数据发布，网络信息内容不断变化，导致了信息传播的时序相关性。</p>
</li>
<li><p>社会性：网络上用户根据自己的需要和喜好发布、回复或转发信息，因而网络数据成了对社会状态的直接反映。</p>
</li>
<li><p>突发性：有些信息在传播过程中会在短时间内引起大量新的网络数据与信息的产生，并使相关的网络用户形成网络群体，体现出网络大数据以及网络群体的突发特性。</p>
</li>
<li><p>高噪声：网络数据来自于众多不同的网络用户，具有很高的噪声。</p>
</li>
</ul>
<h2 id="2-Hadoop-介绍"><a href="#2-Hadoop-介绍" class="headerlink" title="2.Hadoop 介绍"></a>2.Hadoop 介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hadoop是一个开源分布式计算平台框架，基于apache协议发布，由java语言开发。<a href="http://hadoop.apache.org/" target="_blank" rel="external">http://hadoop.apache.org/</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hadoop两大核心组件：<strong>HDFS</strong>（分布式文件系统，为分布式计算提供了数据存储）和<strong>mapreduce</strong>（应用程序被分区成许多小部分，而每个部分都能在集群中的任意节点上运行，一句话就是任务的分解和结果的汇总）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外两个模块：<strong>Common、YARN</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其他和hadoop相关的项目：<strong>Ambari、Avro、Cassandra、Chukwa、Hbase、Hive、Mahout、Pig、Spark、Tez、Zookeeper</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hadoop支持由廉价的计算机搭建集群，有强大的冗余机制。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hadoop在各大互联网企业中应用广泛，百度使用hadoop进行搜索日志的分析和网页数据的挖掘工作；淘宝使用hadoop存储并处理电子商务交易相关数据；facebook使用hadoop进行数据分析和机器学习。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有哪些企业在使用hadoop <a href="http://wiki.apache.org/hadoop/PoweredBy" target="_blank" rel="external">http://wiki.apache.org/hadoop/PoweredBy</a></p>
<h2 id="3-Hadoop组件以及相关项目介绍"><a href="#3-Hadoop组件以及相关项目介绍" class="headerlink" title="3.Hadoop组件以及相关项目介绍"></a>3.Hadoop组件以及相关项目介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Common</strong>：为其他组件提供常用工具支持。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>YARN</strong>：作业调度和集群管理的框架。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Ambari</strong>: 是 Apache Software Foundation 中的一个项目。就 Ambari 的作用来说，就是创建、管理、监视 Hadoop 的集群，但是这里的 Hadoop 是广义，指的是 Hadoop 整个生态圈（例如 Hive，Hbase，Sqoop，Zookeeper 等）。用一句话来说，Ambari 就是为了让 Hadoop 以及相关的大数据软件更容易使用的一个工具。<a href="https://hcldirgit.github.io/2017/08/18/Hadoop/3.%20Ambari%E2%80%94%E2%80%94%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B9%B3%E5%8F%B0%E7%9A%84%E6%90%AD%E5%BB%BA%E5%88%A9%E5%99%A8/" target="_blank" rel="external">Ambari——大数据平台的搭建利器</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Avro</strong>：Avro是Hadoop中的一个子项目，也是Apache中一个独立的项目，Avro是一个基于二进制数据传输高性能的中间件。在Hadoop的其他项目中例如HBase(Ref)和Hive(Ref)的Client端与服务端的数据传输也采用了这个工具。Avro是一个数据序列化的系统。Avro 可以将数据结构或对象转化成便于存储或传输的格式。Avro设计之初就用来支持数据密集型应用，适合于远程或本地大规模数据的存储和交换。<a href="https://hcldirgit.github.io/2017/08/18/Hadoop/4.%20Avro%E7%AE%80%E4%BB%8B/" target="_blank" rel="external">Avro简介</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Cassandra</strong>：可扩展的多主数据库，不存在单点故障。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Chukwa</strong>：是数据收集系统，用于监控和分析大型分布式系统的数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>HBase</strong>：是一个分布式面向列的数据库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Hive</strong>：最早由facebook设计，是建立在hadoop基础之上的数据仓库，它提供了一些用于数据整理、特殊查询和分析在hadoop文件中数据集工具。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Mahout</strong>：可扩展的机器学习和数据挖掘库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Pig</strong>：是一种高级语言和并行计算可执行框架，它是一个对大型数据集分析和评估的平台。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Spark</strong>：一个快速和通用计算的Hadoop数据引擎。和mapreduce类似，但是要比mapreduce快。它提供了一个简单而丰富的编程模型，支持多种应用，包括ETL、机器学习、数据流处理、图形计算。 参考文档 <a href="https://hcldirgit.github.io/2017/08/18/Hadoop/2.%202%E5%88%86%E9%92%9F%E8%AF%BB%E6%87%82Hadoop%E5%92%8CSpark%E7%9A%84%E5%BC%82%E5%90%8C/" target="_blank" rel="external">2分钟读懂Hadoop和Spark的异同</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Tez</strong>：是Apache最新的支持DAG作业的开源计算框架，它可以将多个有依赖的作业转换为一个作业从而大幅提升DAG作业的性能。Tez并不直接面向最终用户，事实上它允许开发者为最终用户构建性能更快、扩展性更好的应用程序。Hadoop传统上是一个大量数据批处理平台。但是，有很多用例需要近乎实时的查询处理性能。还有一些工作则不太适合MapReduce，例如机器学习。Tez的目的就是帮助Hadoop处理这些用例场景。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>ZooKeeper</strong>：ZooKeeper是一组工具，用来配置和支持分布式调度。一个重要功能就是对所有节点进行配置的同步。它能处理分布式应用的“部分失败”问题。部分失败是分布式处理系统的固有特征，即发送者无法知道接收者是否收到消息，它的出现可能和网络传输问题、接收进程意外死掉等有关系。ZooKeeper是Hadoop生态系统的一部分，但又远不止如此，它能支持更多类似的分布式平台和系统，如Jubatus，Cassender等等。而且HBase明确指出至少需要一个ZooKeeper实例的支持。</p>
<h2 id="4-HDFS-介绍"><a href="#4-HDFS-介绍" class="headerlink" title="4.HDFS 介绍"></a>4.HDFS 介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS设计思想来源于Google的GFS，是GFS的开源实现。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS要解决的问题</p>
<ol>
<li>存储超大文件，比如TB级别</li>
<li>防止文件丢失</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS的特点</p>
<ol>
<li>可以存储超大文件</li>
<li>只允许对一个已经打开的文件顺序写入，还可以在现有文件的末尾追加。要想修改一个文件（追加内容除外），只能删除后再重写</li>
<li>可以使用廉价的硬件平台搭建，通过容错策略来保证数据的高可用，默认存储3份数据，任何一份丢失可以自动恢复</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS的缺点</p>
<ol>
<li>数据访问延迟比较高，因为它的设计场景是用于大吞吐量数据，HDFS是单master，所有文件都要经过它，当请求数据量很大时，延迟就增加了</li>
<li>文件数受限，和NameNode有关系</li>
<li>不支持多用户写入，也不支持文件任意修改</li>
</ol>
<h3 id="HDFS-架构"><a href="#HDFS-架构" class="headerlink" title="HDFS 架构"></a>HDFS 架构</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/01.png?raw=true" alt=""></figure></p>
<h4 id="HDFS的几个核心概念"><a href="#HDFS的几个核心概念" class="headerlink" title="HDFS的几个核心概念"></a>HDFS的几个核心概念</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>数据块（block）</strong>：大文件会被分割成多个block进行存储，block大小默认为64MB。每一个block会在多个datanode上存储多份副本，默认是3份。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>namenode</strong>：namenode负责管理文件目录、文件和block的对应关系以及block和datanode的对应关系。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>SecondaryNameNode</strong>：分担namenode的工作量，是NameNode的冷备份，它的主要工作是合并fsimage（元数据镜像文件）和fsedits（元数据操作日志）然后再发给namenode。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>datanode</strong>：datanode就负责存储了，当然大部分容错机制都是在datanode上实现的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>rack</strong> 是指机柜的意思，一个block的三个副本通常会保存到两个或者两个以上的机柜中（当然是机柜中的服务器），这样做的目的是做防灾容错，因为发生一个机柜掉电或者一个机柜的交换机挂了的概率还是蛮高的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;几篇不错的文章</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="https://hcldirgit.github.io/2017/08/18/Hadoop/7.%20%E3%80%90Hadoop%E3%80%91HDFS%E7%9A%84%E8%BF%90%E8%A1%8C%E5%8E%9F%E7%90%86/" target="_blank" rel="external">【Hadoop】HDFS的运行原理</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="https://hcldirgit.github.io/2017/08/18/Hadoop/6.%20HDFS%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%E2%80%94%E2%80%94%20%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" target="_blank" rel="external">HDFS原理分析—— 基本概念</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="https://hcldirgit.github.io/2017/08/18/Hadoop/5.%20HDFS%20%E5%8E%9F%E7%90%86%E3%80%81%E6%9E%B6%E6%9E%84%E4%B8%8E%E7%89%B9%E6%80%A7%E4%BB%8B%E7%BB%8D/" target="_blank" rel="external">HDFS 原理、架构与特性介绍</a></p>
<h2 id="5-HSDS写数据流程"><a href="#5-HSDS写数据流程" class="headerlink" title="5.HSDS写数据流程"></a>5.HSDS写数据流程</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS写文件流程</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Client向远程的Namenode发起RPC请求；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Namenode会检查要创建的文件是否已经存在，创建者是否有权限进行操作，成功则会为文件 创建一个记录，否则会让客户端抛出异常</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当客户端开始写入文件的时候，会将文件切分成多个packets，并向Namenode申请blocks，获取用来存储replicas的合适的datanodes列表，列表的大小根据在Namenode中对replication的设置而定。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时会形成一个pipline用来传输packet。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;packet以流的方式写入第一个datanode，该datanode把packet存储之后，再将其传递给下一个datanode，直到最后一个datanode。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后一个datanode成功存储之后会返回一个ack 传递至客户端，在客户端，客户端确认ack后继续写入下一个packet。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果传输过程中，有某个datanode出现了故障，那么当前的pipeline会被关闭，出现故障的datanode会从当前的pipeline中移除，剩余的block会继续剩下的datanode中继续以pipeline的形式传输，同时Namenode会分配一个新的datanode，保持replicas设定的数量</p>
<h2 id="6-HDFS-读数据流程"><a href="#6-HDFS-读数据流程" class="headerlink" title="6.HDFS 读数据流程"></a>6.HDFS 读数据流程</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HDFS读文件流程</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Client向远程的Namenode发起RPC请求</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Namenode会视情况返回文件的部分或者全部block列表，对于每个block，Namenode都会返回有该block拷贝的DataNode地址</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Client会选取离自己最接近的DataNode来读取block</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;读取完当前block的数据后，关闭与当前的DataNode连接，并为读取下一个block寻找最佳的DataNode</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当读完列表的block后，且文件读取还没有结束，client会继续向Namenode获取下一批的block列表</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;读取完block会进行checksum验证，如果读取datanode时出现错误，客户端会通知Namenode，然后再从下一个拥有该block拷贝的datanode继续读</p>
<h2 id="7-mapreduce-详解"><a href="#7-mapreduce-详解" class="headerlink" title="7.mapreduce 详解"></a>7.mapreduce 详解</h2><h3 id="MapReduce模型"><a href="#MapReduce模型" class="headerlink" title="MapReduce模型"></a>MapReduce模型</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MapReduce 是大规模数据（TB 级）计算的利器，Map 和Reduce 是它的主要思想，来源于函数式编程语言。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Map负责将数据打散，Reduce负责对数据进行聚集，用户只需要实现map 和reduce 两个接口，即可完成TB级数据的计算。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;常见的应用包括：日志分析和数据挖掘等数据分析应用。另外，还可用于科学数据计算，如圆周率PI 的计算等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当我们提交一个计算作业时，MapReduce会首先把计算作业拆分成若干个Map 任务，然后分配到不同的节点上去执行，每一个Map 任务处理输入数据中的一部分，当Map 任务完成后，它会生成一些中间文件，这些中间文件将会作为Reduce 任务的输入数据。Reduce 任务的主要目标就是把前面若干个Map 的输出汇总到一起并输出</p>
<h3 id="MapReduce-执行过程"><a href="#MapReduce-执行过程" class="headerlink" title="MapReduce 执行过程"></a>MapReduce 执行过程</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个 Mapper 任务是一个 java 进程，它会读取 HDFS 中的文件，解析成很多的键值对，经过我们 map 方法处理后， 转换为很多的键值对再输出</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把 Mapper 任务的运行过程分为六个阶段。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一阶段是把输入文件按照一定的标准分片(InputSplit)，每个输入片的大小是固定的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第二阶段是对输入片中的记录按照一定的规则解析成键值对。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第三阶段是调用 Mapper 类中的 map 方法。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第四阶段是按照一定的规则对第三阶段输出的键值对进行分区。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第五阶段是对每个分区中的键值对进行排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第六阶段是对数据进行归纳处理，也就是 reduce 处理。键相等的键值对会调用一次reduce 方法。</p>
<h3 id="Reducer任务的执行过程"><a href="#Reducer任务的执行过程" class="headerlink" title="Reducer任务的执行过程"></a>Reducer任务的执行过程</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个 Reducer 任务是一个 java 进程。Reducer 任务接收 Mapper 任务的输出，归约处理后写入到 HDFS 中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以分为3个阶段</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一阶段是 Reducer 任务会主动从 Mapper 任务复制其输出的键值对。 Mapper 任务可能会有很多，因此 Reducer 会复制多个 Mapper 的输出。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第二阶段是把复制到 Reducer 本地数据，全部进行合并，即把分散的数据合并成一个大的数据。再对合并后的数据排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第三阶段是对排序后的键值对调用 reduce 方法。 键相等的键值对调用一次 reduce 方法，每次调用会产生零个或者多个键值对。最后把这些输出的键值对写入到 HDFS 文件中。</p>
<h2 id="8-安装-hadoop–准备工作"><a href="#8-安装-hadoop–准备工作" class="headerlink" title="8.安装 hadoop–准备工作"></a>8.安装 hadoop–准备工作</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;三台机器（内存大于2G） 分别写hosts、设定hostname。三台机器分别设置hostname 为master 、slave1、slave2。更改 hosts</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">vim /etc/hosts</div><div class="line">``` </div><div class="line"></div><div class="line">```bash</div><div class="line">192.168.0.87 master</div><div class="line">192.168.0.86 slave1</div><div class="line">192.168.0.85 slave2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭selinux，关闭firewalld</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># systemctl disable firewalld</span></div><div class="line">Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.</div><div class="line">Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.</div><div class="line">[root@master ~]<span class="comment"># systemctl stop firewalld</span></div><div class="line">[root@master ~]<span class="comment"># yum install -y iptables-services</span></div><div class="line"> </div><div class="line">[root@master ~]<span class="comment"># systemctl enable iptables</span></div><div class="line">Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.</div><div class="line">[root@master ~]<span class="comment"># systemctl start iptables</span></div><div class="line">[root@master ~]<span class="comment"># iptables -F</span></div><div class="line">[root@master ~]<span class="comment"># service iptables save</span></div><div class="line">iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上操作三台机器都需执行。</p>
<h2 id="9-安装-hadoop–密钥认证"><a href="#9-安装-hadoop–密钥认证" class="headerlink" title="9.安装 hadoop–密钥认证"></a>9.安装 hadoop–密钥认证</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master可以通过密钥登陆本机和两台slave</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上生成密钥对：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ssh-keygen 一直回车</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ssh-keygen</span></div><div class="line">Generating public/private rsa key pair.</div><div class="line">Enter file <span class="keyword">in</span> <span class="built_in">which</span> to save the key (/root/.ssh/id_rsa): </div><div class="line">Created directory <span class="string">'/root/.ssh'</span>.</div><div class="line">Enter passphrase (empty <span class="keyword">for</span> no passphrase): </div><div class="line">Enter same passphrase again: </div><div class="line">Your identification has been saved <span class="keyword">in</span> /root/.ssh/id_rsa.</div><div class="line">Your public key has been saved <span class="keyword">in</span> /root/.ssh/id_rsa.pub.</div><div class="line">The key fingerprint is:</div><div class="line">29:86:0f:70:28:2b:0a:9e:9c:98:39:dc:d2:c2:5b:d4 root@master</div><div class="line">The key<span class="string">'s randomart image is:</span></div><div class="line"><span class="string">+--[ RSA 2048]----+</span></div><div class="line"><span class="string">|                 |</span></div><div class="line"><span class="string">|   .             |</span></div><div class="line"><span class="string">|. o .            |</span></div><div class="line"><span class="string">| o o..    .      |</span></div><div class="line"><span class="string">|+  .oEo  S       |</span></div><div class="line"><span class="string">|Xo*  +  .        |</span></div><div class="line"><span class="string">|*X +  .          |</span></div><div class="line"><span class="string">| .=              |</span></div><div class="line"><span class="string">| .               |</span></div><div class="line"><span class="string">+-----------------+</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;复制~/.ssh/id_rsa.pub 内容到本机和两台slave的 ~/.ssh/authorized_keys</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># cat .ssh/id_rsa.pub</span></div><div class="line">ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZfOQKxMrCOf95iZvdNkTg32nQeUp3rywF+d0SS+t5ccZ0YjbZUZVFOkh5Sg5gdsjLgJoduZDePtYYhbex1kKPs8E6cx073ZqpW37TBGObCv7Inz1Ks+TSplnw/AKH6uRTEswC5P2SD+mJ+iz+OTgsNJyrj+OGGH1gOhmzQuAznSChqkJaihNhcBOOuJf8rVqhmplN9YPuGBlGc3It6uFHZvw8C42bC7xyqobL3FRZwKw85WQ9ZjdPTKQzg5rcn76gCld9fRuWkL1ABbP6MRIawN5eonYMYVS05PUGVadHM+a9L5nwTAbA4YqGyQ0m37mHV+5BwaBHxQyY5RSIiiyH root@master</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># vim .ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置本机和两台slave机器上的~/.ssh/authorized_keys文件权限为600</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># chmod 600 ~/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在master上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ssh master</span></div><div class="line">Last failed login: Tue Jan 10 16:58:22 CST 2017 from master on ssh:notty</div><div class="line">There were 8 failed login attempts since the last successful login.</div><div class="line">Last login: Tue Jan 10 16:53:03 2017 from 192.168.0.100</div><div class="line">[root@master ~]<span class="comment"># 登出</span></div><div class="line">Connection to master closed.</div><div class="line">[root@master ~]<span class="comment"># ssh slave1</span></div><div class="line">Last login: Tue Jan 10 16:52:18 2017 from master</div><div class="line">[root@slave1 ~]<span class="comment"># 登出</span></div><div class="line">Connection to slave1 closed.</div><div class="line">[root@master ~]<span class="comment"># ssh slave2</span></div><div class="line">Last login: Tue Jan 10 16:55:40 2017 from master</div><div class="line">[root@slave2 ~]<span class="comment"># 登出</span></div><div class="line">Connection to slave2 closed.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以直接登陆</p>
<h2 id="10-安装-hadoop-–安装-JDK"><a href="#10-安装-hadoop-–安装-JDK" class="headerlink" title="10.安装 hadoop –安装 JDK"></a>10.安装 hadoop –安装 JDK</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hadoop2.7 需要安装jdk1.7版本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址<a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html" target="_blank" rel="external">http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压压缩包 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># tar zxvf jdk-8u111-linux-x64.tar.gz</span></div><div class="line">[root@master ~]<span class="comment"># mv jdk1.8.0_111 /usr/local/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写环境变量配置 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># vim /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.7.0_79</div><div class="line"><span class="built_in">export</span> CLASSPATH=.:<span class="variable">$JAVA_HOME</span>/jre/lib/rt.jar:<span class="variable">$JAVA_HOME</span>/lib/dt.jar:<span class="variable">$JAVA_HOME</span>/li</div><div class="line">b/tools.jar</div><div class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$JAVA_HOME</span>/bin</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># source /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;java -version 查看是否生效</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># java -version</span></div><div class="line">java version <span class="string">"1.8.0_111"</span></div><div class="line">Java(TM) SE Runtime Environment (build 1.8.0_111-b14)</div><div class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave1 和 slave2 重复上面的操作</p>
<h2 id="11-安装hadooop-安装-hadoop-包"><a href="#11-安装hadooop-安装-hadoop-包" class="headerlink" title="11.安装hadooop-安装 hadoop 包"></a>11.安装hadooop-安装 hadoop 包</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下操作在master上执行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址 <a href="http://hadoop.apache.org/releases.html" target="_blank" rel="external">http://hadoop.apache.org/releases.html</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;镜像站 <a href="http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.1/" target="_blank" rel="external">http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.1/</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载2.7.1 binary版本  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.1/hadoop-2.7.1.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压 tar zxf hadoop-2.7.1.tar.gz</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># tar zxf hadoop-2.7.1.tar.gz</span></div><div class="line">[root@master ~]<span class="comment"># mv hadoop-2.7.1 /usr/local/hadoop</span></div><div class="line">[root@master ~]<span class="comment"># cd /usr/local/hadoop</span></div><div class="line">[root@master hadoop]<span class="comment"># mkdir tmp dfs dfs/data dfs/name</span></div><div class="line">[root@master hadoop]<span class="comment"># ls</span></div><div class="line">bin etc lib LICENSE.txt README.txt share</div><div class="line">dfs include libexec NOTICE.txt sbin tmp</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把/usr/local/hadoop 目录分别拷贝至两个slave上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># rsync -av /usr/local/hadoop slave1:/usr/local/</span></div><div class="line">[root@master hadoop]<span class="comment"># rsync -av /usr/local/hadoop slave2:/usr/local/</span></div></pre></td></tr></table></figure>
<h2 id="12-安装-hadoop-配置-hadoop"><a href="#12-安装-hadoop-配置-hadoop" class="headerlink" title="12.安装 hadoop -配置 hadoop"></a>12.安装 hadoop -配置 hadoop</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上 vim /usr/local/hadoop/etc/hadoop/core-site.xml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># vim /usr/local/hadoop/etc/hadoop/core-site.xml</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;configuration&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;fs.defaultFS&lt;/name&gt;</div><div class="line">        &lt;value&gt;hdfs://192.168.0.87:9000&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;hadoop.tmp.dir&lt;/name&gt;</div><div class="line">        &lt;value&gt;file:/usr/<span class="built_in">local</span>/hadoop/tmp&lt;/value&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;io.file.buffer.size&lt;/name&gt;</div><div class="line">        &lt;value&gt;131702&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上 vim /usr/local/hadoop/etc/hadoop/hdfs-site.xml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># vim /usr/local/hadoop/etc/hadoop/hdfs-site.xml</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">&lt;configuration&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;dfs.namenode.name.dir&lt;/name&gt;</div><div class="line">        &lt;value&gt;file:/usr/<span class="built_in">local</span>/hadoop/dfs/name&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;dfs.datanode.data.dir&lt;/name&gt;</div><div class="line">        &lt;value&gt;file:/usr/<span class="built_in">local</span>/hadoop/dfs/data&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;dfs.replication&lt;/name&gt;</div><div class="line">        &lt;value&gt;2&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;dfs.namenode.secondary.http-address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:9001&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;dfs.webhdfs.enabled&lt;/name&gt;</div><div class="line">        &lt;value&gt;<span class="literal">true</span>&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上 vim /usr/local/hadoop/etc/hadoop/mapred-site.xml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># mv /usr/local/hadoop/etc/hadoop/mapred-site.xml.template /usr/local/hadoop/etc/hadoop/mapred-site.xml</span></div><div class="line">[root@master hadoop]<span class="comment"># vim /usr/local/hadoop/etc/hadoop/mapred-site.xml</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;configuration&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;mapreduce.framework.name&lt;/name&gt;</div><div class="line">        &lt;value&gt;yarn&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;mapreduce.jobhistory.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;172.7.15.113:10020&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;mapreduce.jobhistory.webapp.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:19888&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上 vim /usr/local/hadoop/etc/hadoop/yarn-site.xml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># vim /usr/local/hadoop/etc/hadoop/yarn-site.xml</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">&lt;configuration&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.nodemanager.aux-services&lt;/name&gt;</div><div class="line">        &lt;value&gt;mapreduce_shuffle&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.nodemanager.auxservices.mapreduce.shuffle.class&lt;/name&gt;</div><div class="line">        &lt;value&gt;org.apache.hadoop.mapred.ShuffleHandler&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.resourcemanager.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:8032&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.resourcemanager.scheduler.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:8030&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.resourcemanager.resource-tracker.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:8031&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.resourcemanager.admin.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:8033&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.resourcemanager.webapp.address&lt;/name&gt;</div><div class="line">        &lt;value&gt;192.168.0.87:8088&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">    &lt;property&gt;</div><div class="line">        &lt;name&gt;yarn.nodemanager.resource.memory-mb&lt;/name&gt;</div><div class="line">        &lt;value&gt;2048&lt;/value&gt;</div><div class="line">    &lt;/property&gt;</div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下在master上操作</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改JAVA_HOME</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># cd /usr/local/hadoop/etc/hadoop</span></div><div class="line">[root@master hadoop]<span class="comment"># vim hadoop-env.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改JAVA_HOME</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># vim yarn-env.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Hadoop%20%E5%A4%A7%E6%95%B0%E6%8D%AE/08.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slaves 文件修改</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># vim slaves</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">192.168.0.86</div><div class="line">192.168.0.85</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将master上的etc目录同步至两个slave</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># rsync -av /usr/local/hadoop/etc/ slave1:/usr/local/hadoop/etc/</span></div><div class="line">sending incremental file list</div><div class="line">hadoop/</div><div class="line">hadoop/core-site.xml</div><div class="line">hadoop/hadoop-env.sh</div><div class="line">hadoop/hdfs-site.xml</div><div class="line">hadoop/mapred-site.xml</div><div class="line">hadoop/slaves</div><div class="line">hadoop/yarn-env.sh</div><div class="line">hadoop/yarn-site.xml</div><div class="line"> </div><div class="line">sent 6527 bytes received 269 bytes 13592.00 bytes/sec</div><div class="line">total size is 79165 speedup is 11.65</div><div class="line">[root@master hadoop]<span class="comment"># rsync -av /usr/local/hadoop/etc/ slave2:/usr/local/hadoop/etc/</span></div><div class="line">sending incremental file list</div><div class="line">hadoop/</div><div class="line">hadoop/core-site.xml</div><div class="line">hadoop/hadoop-env.sh</div><div class="line">hadoop/hdfs-site.xml</div><div class="line">hadoop/mapred-site.xml</div><div class="line">hadoop/slaves</div><div class="line">hadoop/yarn-env.sh</div><div class="line">hadoop/yarn-site.xml</div><div class="line"> </div><div class="line">sent 6527 bytes received 269 bytes 13592.00 bytes/sec</div><div class="line">total size is 79165 speedup is 11.65</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在master上操作即可，两个slave会自动启动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;初始化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># /usr/local/hadoop/bin/hdfs namenode -format</span></div><div class="line">[root@master hadoop]<span class="comment"># echo $?</span></div><div class="line">0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动服务 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># /usr/local/hadoop/sbin/start-all.sh</span></div><div class="line">This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh</div><div class="line">Starting namenodes on [master]</div><div class="line">master: starting namenode, logging to /usr/<span class="built_in">local</span>/hadoop/logs/hadoop-root-namenode-master.out</div><div class="line">192.168.0.85: starting datanode, logging to /usr/<span class="built_in">local</span>/hadoop/logs/hadoop-root-datanode-slave2.out</div><div class="line">192.168.0.86: starting datanode, logging to /usr/<span class="built_in">local</span>/hadoop/logs/hadoop-root-datanode-slave1.out</div><div class="line">Starting secondary namenodes [master]</div><div class="line">master: starting secondarynamenode, logging to /usr/<span class="built_in">local</span>/hadoop/logs/hadoop-root-secondarynamenode-master.out</div><div class="line">starting yarn daemons</div><div class="line">starting resourcemanager, logging to /usr/<span class="built_in">local</span>/hadoop/logs/yarn-root-resourcemanager-master.out</div><div class="line">192.168.0.85: starting nodemanager, logging to /usr/<span class="built_in">local</span>/hadoop/logs/yarn-root-nodemanager-slave2.out</div><div class="line">192.168.0.86: starting nodemanager, logging to /usr/<span class="built_in">local</span>/hadoop/logs/yarn-root-nodemanager-slave1.out</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;停止服务 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># /usr/local/hadoop/sbin/stop-all.sh</span></div><div class="line">This script is Deprecated. Instead use stop-dfs.sh and stop-yarn.sh</div><div class="line">Stopping namenodes on [master]</div><div class="line">master: stopping namenode</div><div class="line">192.168.0.85: stopping datanode</div><div class="line">192.168.0.86: stopping datanode</div><div class="line">Stopping secondary namenodes [master]</div><div class="line">master: stopping secondarynamenode</div><div class="line">stopping yarn daemons</div><div class="line">stopping resourcemanager</div><div class="line">192.168.0.85: no nodemanager to stop</div><div class="line">192.168.0.86: no nodemanager to stop</div><div class="line">no proxyserver to stop</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;访问</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浏览器打开<a href="http://192.168.0.87:8088" target="_blank" rel="external">http://192.168.0.87:8088</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浏览器打开<a href="http://192.168.0.87:50070" target="_blank" rel="external">http://192.168.0.87:50070</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以在 master、slave1、slave2 上执行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># ps aux |grep java</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># netstat -lnp |grep java</span></div><div class="line">tcp  0  0  0.0.0.0:50070      0.0.0.0:*  LISTEN  3626/java </div><div class="line">tcp  0  0  192.168.0.87:9000  0.0.0.0:*  LISTEN  3626/java </div><div class="line">tcp  0  0  192.168.0.87:9001  0.0.0.0:*  LISTEN  3820/java </div><div class="line">tcp6 0  0  192.168.0.87:8088  :::*       LISTEN  3985/java </div><div class="line">tcp6 0  0  192.168.0.87:8030  :::*       LISTEN  3985/java </div><div class="line">tcp6 0  0  192.168.0.87:8031  :::*       LISTEN  3985/java </div><div class="line">tcp6 0  0  192.168.0.87:8032  :::*       LISTEN  3985/java </div><div class="line">tcp6 0  0  192.168.0.87:8033  :::*       LISTEN  3985/java</div></pre></td></tr></table></figure>
<h2 id="13-测试-hadoop"><a href="#13-测试-hadoop" class="headerlink" title="13.测试 hadoop"></a>13.测试 hadoop</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下操作在master上实现</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;建立测试目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># cd /usr/local/hadoop</span></div><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -mkdir /123</span></div><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -ls /</span></div><div class="line">Found 1 items</div><div class="line">drwxr-xr-x     - root supergroup       0 2017-01-11 17:22 /123</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果提示 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">copyFromLocal: Cannot create directory /123/. Name node is <span class="keyword">in</span> safe mode.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是因为开启了安全模式，解决办法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfsadmin -safemode leave</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将当前目录下的LICENSE.txt复制到hadopp中，查看/123/下有哪些文件  bin/hdfs dfs -ls /123</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -copyFromLocal ./LICENSE.txt /123 </span></div><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -ls /123</span></div><div class="line">Found 1 items</div><div class="line">-rw-r--r-- 2 root supergroup 15429 2017-01-11 17:29 /123/LICENSE.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用wordcount分析LICENSE.txt</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.1.jar wordcount /123/LICENSE.txt /output/123</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bin/hdfs dfs -ls /output/123  查看分析后的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -ls /output/123</span></div><div class="line">Found 2 items</div><div class="line">-rw-r--r-- 2 root supergroup 0 2017-01-11 17:47 /output/123/_SUCCESS</div><div class="line">-rw-r--r-- 2 root supergroup 8006 2017-01-11 17:47 /output/123/part-r-00000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bin/hdfs dfs -cat /output/123/part-r-00000  查看分析结果</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master hadoop]<span class="comment"># bin/hdfs dfs -cat /output/123/part-r-00000</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Git/3. git,github,gitlab/">git,github,gitlab</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Git/">Git</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果初次接触git的朋友一定搞不清git，github以及gitlab这几个概念。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git如果不知道是啥，那你总知道svn是啥吧。什么？svn也不知道？那只好辛苦你动动手搜一下了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git用一句话介绍：它是一个分布式的版本管理工具。通过两张图来对比svn和git的最主要的差异——分布式。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/git,github,gitlab/01.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/git,github,gitlab/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上面两个图，可以看出使用svn的版本变化数据都是存在于服务端的，最终控制中心只有一个。而git，每一个客户端都可以作为一个版本管理中心，当然它也需要有一个公共的总控制点。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再给大家举一个例子吧，比如我和朋友一起开发一个软件，这个软件有很多代码片段，所以改动也比较频繁。如果使用svn，那我们无论谁每改动一段代码都要推送到svn中心去，而为了保证代码统一，其中一个人只要因为推送新的代码变动了控制中心，另一个人都要把改动的地方拉取到自己的电脑。所以这样就比较麻烦了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而使用git时，我和朋友都可以在自己电脑上搞一个git控制中心，我们电脑里面的代码可以不一致，只需要用自己的git管理自己的那一部分代码。而git服务端只是用来合并我和朋友的最终版本代码的，平时的小改动由我们自己电脑里面的控制中心来管理，git服务端不用关心。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面我用一个公司老总管理员工的例子再来总结一下svn和git的差异。svn架构里面的管理者比较独裁，不信任任何人，所有的事情都是亲力亲为，它宁愿管理所有员工也不设置下级管理者。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而git架构里面的老总比较明智，他比较偷懒，只管大事情，小事情都是交给下面的小leader去搞定的。每个小leader去管理自己的下属也不用关心其他leader的事情。但是所有leader都需要不定期向大boss汇报一下近期的情况。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;github，是一个web界面的git管理平台，也就是说它底层的技术驱动依然是git。一句话区分，git是管理工具，github是在线的基于git的平台（或者叫做服务）。github的角色也就是我们上面例子中的大boss。github是公开的，谁都可以注册账号，并创建自己的项目，大部分项目以代码为主，比如wordpress、discuz都是托管在github里面的，当然还有好多好多开源的软件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;既然github是一个公共的，在线的提供服务的平台，那企业就可以把公司内部的代码托管在这里，为了安全我们可以选择付费服务，定制个性化方案。但毕竟github在国外，要想获得更好的功能还需要花钱，小企业或者个人用户自然是不愿意去开销了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还好又一款开源的软件可以帮我们搭建一套类似github的平台，这就是接下来我要介绍的gitlab。 gitlab适合企业内部构建私有代码管理平台，它有健全的权限控制系统和各种强大的功能，很赞。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以，gitlab和github一样，也是一个基于git的提供web界面的代码管理平台。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Git/2.  github常见操作和常见错误  错误提示  fatal   remote origin already exists./">github常见操作和常见错误  错误提示  fatal   remote origin already exists.</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Git/">Git</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果输入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Git remote add origin git@github.com:djqiang（github帐号名）/gitdemo（项目名）.git</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提示出错信息：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">fatal: remote origin already exists.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法如下：</p>
<ol>
<li><p>先输入 <strong>$ git remote rm origin</strong></p>
</li>
<li><p>再输入 $ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了！</p>
</li>
<li><p>如果输入 <strong>$ git remote rm origin</strong> 还是报错的话，error: Could not remove config section ‘remote.origin’. 我们需要修改gitconfig文件的内容</p>
</li>
<li><p>找到github的安装路径，我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc</p>
</li>
<li><p>找到一个名为gitconfig的文件，打开它把里面的[remote “origin”]那一行删掉就好了！</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果输入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ssh -T git@github.com</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出现错误提示：Permission denied (publickey).因为新生成的key不能加入ssh就会导致连接不上github。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法如下：</p>
<ol>
<li><p>先输入$ ssh-agent，再输入$ ssh-add ~/.ssh/id_key，这样就可以了。</p>
</li>
<li><p>如果还是不行的话，输入ssh-add ~/.ssh/id_key 命令后出现报错Could not open a connection to your authentication agent.解决方法是key用Git Gui的ssh工具生成，这样生成的时候key就直接保存在ssh中了，不需要再ssh-add命令加入了，其它的user，token等配置都用命令行来做。</p>
</li>
<li><p>最好检查一下在你复制id_rsa.pub文件的内容时有没有产生多余的空格或空行，有些编辑器会帮你添加这些的。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果输入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git push origin master</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提示出错信息：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">error:failed to push som refs to .......</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法如下：</p>
<ol>
<li><p>先输入$ git pull origin master //先把远程服务器github上面的文件拉下来</p>
</li>
<li><p>再输入$ git push origin master</p>
</li>
<li><p>如果出现报错 fatal: Couldn’t find remote ref master或者fatal: ‘origin’ does not appear to be a git repository以及fatal: Could not read from remote repository.</p>
</li>
<li><p>则需要重新输入$ git remote add origingit@github.com:djqiang/gitdemo.git</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用git在本地创建一个项目的过程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">makdir ~/hello-world    //创建一个项目hello-world</div><div class="line"><span class="built_in">cd</span> ~/hello-world       //打开这个项目</div><div class="line">git init             //初始化 </div><div class="line">touch README</div><div class="line">git add README        //更新README文件</div><div class="line">git commit -m <span class="string">'first commit'</span>     //提交更新，并注释信息“first commit” </div><div class="line">git remote add origin git@github.com:defnngj/hello-world.git     //连接远程github项目  </div><div class="line">git push -u origin master     //将本地项目更新到github项目上去</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;gitconfig配置文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Git有一个工具被称为git config，它允许你获得和设置配置变量；这些变量可以控制Git的外观和操作的各个方面。这些变量可以被存储在三个不同的位置： </p>
<ol>
<li><p>/etc/gitconfig 文件：包含了适用于系统所有用户和所有库的值。如果你传递参数选项’–system’ 给 git config，它将明确的读和写这个文件。 </p>
</li>
<li><p>~/.gitconfig 文件 ：具体到你的用户。你可以通过传递–global 选项使Git 读或写这个特定的文件。</p>
</li>
<li><p>位于git目录的config文件 (也就是 .git/config) ：无论你当前在用的库是什么，特定指向该单一的库。每个级别重写前一个级别的值。因此，在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Windows系统中，Git在$HOME目录中查找.gitconfig文件（对大多数人来说，位于C:\Documents and Settings\$USER下）。它也会查找/etc/gitconfig，尽管它是相对于Msys 根目录的。这可能是你在Windows中运行安装程序时决定安装Git的任何地方。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置相关信息：</p>
<ol>
<li>当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。这是非常重要的，因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">git config --global user.name <span class="string">"John Doe"</span></div><div class="line">git config --global user.email johndoe@example.com</div><div class="line">``` </div><div class="line"></div><div class="line">2. 你的编辑器(Your Editor)</div><div class="line"></div><div class="line">&amp;<span class="comment">#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;现在，标识已经设置，可以配置缺省文本编辑器，Git在需要输入一些消息时会使用该文本编辑器。缺省情况下，Git使用系统的缺省编辑器，这通常可能是vi 或者 vim。如果想使用一个不同的文本编辑器，例如Emacs，可以做如下操作：</span></div><div class="line"></div><div class="line">```bash</div><div class="line">git config --global core.editor emacs</div></pre></td></tr></table></figure>
<ol>
<li>检查设置(Checking Your Settings)</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想检查设置，可以使用 git config –list 命令来列出Git可以在该处找到的所有的设置:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git config --list</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以查看Git认为的一个特定的关键字目前的值，使用如下命令 git config {key}:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git config user.name</div></pre></td></tr></table></figure>
<ol>
<li>获取帮助(Getting help)</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果当你在使用Git时需要帮助，有三种方法可以获得任何git命令的手册页(manpage)帮助信息:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">git <span class="built_in">help</span> &lt;verb&gt;</div><div class="line">git &lt;verb&gt; --<span class="built_in">help</span></div><div class="line">man git-&lt;verb&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如，你可以运行如下命令获取对config命令的手册页帮助:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git <span class="built_in">help</span> config</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Git/1. Git安装使用/">Git安装使用</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Git/">Git</a>
			</span>
		
	</div>

	

	
		<h2 id="1-安装git"><a href="#1-安装git" class="headerlink" title="1.安装git"></a>1.安装git</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos上  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># yum install -y epel-release</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># yum install -y git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ubuntu上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo apt-get install git</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;windows 上安装 msysgit,<a href="https://git-for-windows.github.io/" target="_blank" rel="external">下载地址</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完成后，还需要最后一步设置  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># git config --global user.name "yanyi"</span></div><div class="line">[root@localhost ~]<span class="comment"># git config --global user.email "hcldir@qq.com"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置完成会生成 .gitconfig 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls -la</span></div><div class="line">-rw-r--r-- 1 root root 44 1月 6 20:01 .gitconfig</div><div class="line">[root@localhost ~]<span class="comment"># cat .gitconfig </span></div><div class="line">[user]</div><div class="line">      name = yanyi</div><div class="line">      email = hcldir@qq.com</div></pre></td></tr></table></figure>
<h2 id="2-创建版本库"><a href="#2-创建版本库" class="headerlink" title="2.创建版本库"></a>2.创建版本库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># mkdir /home/gitroot</span></div><div class="line">[root@localhost ~]<span class="comment"># cd /home/gitroot</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git init</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /home/gitroot/.git/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：git init 用这个命令初始化，让这个目录变成git可以管理的仓库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ls -a 可以看到多了一个 .git 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># ls -a</span></div><div class="line">. .. .git</div></pre></td></tr></table></figure>
<h2 id="3-提交文件到仓库"><a href="#3-提交文件到仓库" class="headerlink" title="3.提交文件到仓库"></a>3.提交文件到仓库</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个文件 1.txt</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># echo -e "123\naaa\n456\nbbb" &gt; 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># cat 1.txt</span></div><div class="line">123</div><div class="line">aaa</div><div class="line">456</div><div class="line">bbb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把1.txt添加到仓库中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git add 1.txt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>add</strong> 完了必须要 <strong>commit</strong> 才算真正把文件提交到git仓库里</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m "add new file 1.txt"</span></div><div class="line">[master (root-commit) 0214d82] add new file 1.txt</div><div class="line"> 1 files changed, 4 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改 1.txt后，查看和版本库是否一致</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line"><span class="comment"># Changed but not updated:</span></div><div class="line"><span class="comment"># (use "git add &lt;file&gt;..." to update what will be committed)</span></div><div class="line"><span class="comment"># (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># modified: 1.txt</span></div><div class="line"><span class="comment">#</span></div><div class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：<strong>git status</strong>  查看当前仓库中的状态，比如是否有改动的文件  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不提交，需要把文件恢复成版本库里的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git checkout -- 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># cat 1.txt</span></div><div class="line">123</div><div class="line">aaa</div><div class="line">456</div><div class="line">bbb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次执行 git status 命令，提示没有任何更改</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line">nothing to commit (working directory clean)</div><div class="line">``` </div><div class="line"></div><div class="line">&amp;<span class="comment">#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;再次修改 1.txt文件，对比修改内容</span></div><div class="line"></div><div class="line">```bash</div><div class="line">[root@localhost gitroot]<span class="comment"># echo -e "111111\n2222222222" &gt;&gt; 1.txt </span></div><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line"><span class="comment"># Changed but not updated:</span></div><div class="line"><span class="comment"># (use "git add &lt;file&gt;..." to update what will be committed)</span></div><div class="line"><span class="comment"># (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># modified: 1.txt</span></div><div class="line"><span class="comment">#</span></div><div class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</div><div class="line">[root@localhost gitroot]<span class="comment"># git diff 1.txt</span></div><div class="line">diff --git a/1.txt b/1.txt</div><div class="line">index b149eee..aa2d2ae 100644</div><div class="line">--- a/1.txt</div><div class="line">+++ b/1.txt</div><div class="line">@@ -2,3 +2,5 @@</div><div class="line"> aaa</div><div class="line"> 456</div><div class="line"> bbb</div><div class="line">+111111</div><div class="line">+2222222222</div></pre></td></tr></table></figure>
<p>说明：<strong>git diff</strong> 1.txt  可以对比1.txt本次修改了什么内容，相比较仓库里面的版本。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把更改个文件提交到版本库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git add 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m "add a line 1.txt"</span></div><div class="line">[master 182c98f] add a line 1.txt</div><div class="line">1 files changed, 2 insertions(+), 0 deletions(-)</div><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line">nothing to commit (working directory clean)</div></pre></td></tr></table></figure>
<h2 id="4-版本回退"><a href="#4-版本回退" class="headerlink" title="4.版本回退"></a>4.版本回退</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;多更改几次1.txt，并进行 git add,  git commit操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># echo "asdfkjsadkjf" &gt;&gt; 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git add 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m "change 1.txt agin"</span></div><div class="line">[master 0c72aba] change 1.txt agin</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line">[root@localhost gitroot]<span class="comment"># echo "123456" &gt;&gt; 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git add 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m "change 1.txt agin agin"</span></div><div class="line">[master e4fe595] change 1.txt agin agin</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line">nothing to commit (working directory clean)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git log</strong>  查看所有提交git仓库的记录操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git log</span></div><div class="line">commit e4fe59596fc6c16757cb1d772d775932701f86ab</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date:   Fri Jan 6 21:57:43 2017 +0800</div><div class="line"></div><div class="line">     change 1.txt agin agin</div><div class="line"></div><div class="line">commit 0c72aba21aba27ccc9c9873ec53c552d5474a02f</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date:   Fri Jan 6 21:56:41 2017 +0800</div><div class="line"></div><div class="line">     change 1.txt agin</div><div class="line"></div><div class="line">commit 182c98ff8798f47965ba44ed218e9da5e9964295</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date:   Fri Jan 6 20:34:15 2017 +0800</div><div class="line"></div><div class="line">     add a line 1.txt</div><div class="line"></div><div class="line">commit 0214d8252fd3c37a7f09fc32e8ff29d10d577230</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date:   Fri Jan 6 20:15:19 2017 +0800</div><div class="line"></div><div class="line">     add new file 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;根据这个 log 可退回到前边某个版本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git log –pretty=oneline</strong> 命令可以让每个版本单独一行显示，更清晰。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git log --pretty=oneline</span></div><div class="line">e4fe59596fc6c16757cb1d772d775932701f86ab change 1.txt agin agin</div><div class="line">0c72aba21aba27ccc9c9873ec53c552d5474a02f change 1.txt agin</div><div class="line">182c98ff8798f47965ba44ed218e9da5e9964295 add a line 1.txt</div><div class="line">0214d8252fd3c37a7f09fc32e8ff29d10d577230 add new file 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git reset –hard 字符串</strong> 可以退回到以前版本，字符串可以简写，只写前边几位</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git reset --hard 182c</span></div><div class="line">HEAD is now at 182c98f add a line 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;退回版本后，在 git log 则无法显示退回版本以上的版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git log --pretty=oneline</span></div><div class="line">182c98ff8798f47965ba44ed218e9da5e9964295 add a line 1.txt</div><div class="line">0214d8252fd3c37a7f09fc32e8ff29d10d577230 add new file 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git reflog</strong> 可以显示所有版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git reflog</span></div><div class="line">e4fe595 HEAD@&#123;0&#125;: e4fe5: updating HEAD</div><div class="line">182c98f HEAD@&#123;1&#125;: 182c: updating HEAD</div><div class="line">e4fe595 HEAD@&#123;2&#125;: commit: change 1.txt agin agin</div><div class="line">0c72aba HEAD@&#123;3&#125;: commit: change 1.txt agin</div><div class="line">182c98f HEAD@&#123;4&#125;: commit: add a line 1.txt</div><div class="line">0214d82 HEAD@&#123;5&#125;: commit (initial): add new file 1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后可以使用 git reset –hard 字符串 再次退回版本。</p>
<h2 id="5-撤销修改（文件恢复）"><a href="#5-撤销修改（文件恢复）" class="headerlink" title="5.撤销修改（文件恢复）"></a>5.撤销修改（文件恢复）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果修改文件后，发现改的不对，想恢复到上一次提交的状态，或不小心删除了文件。可以使用</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git checkout – file</strong> 恢复到上一次提交的状态。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git checkout 1.txt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果文件修改完成，保存后，git add 了，但没有 commit ，想退回到上一次提交的状态可以用<strong>git reset HEAD <file></file></strong>，再使用 git checkout – file </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git add 1.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line"><span class="comment"># Changes to be committed:</span></div><div class="line"><span class="comment"># (use "git reset HEAD &lt;file&gt;..." to unstage)</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># modified: 1.txt</span></div><div class="line"><span class="comment">#</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git reset HEAD 1.txt</span></div><div class="line">Unstaged changes after reset:</div><div class="line">M 1.txt</div><div class="line">[root@localhost gitroot]<span class="comment"># git checkout -- 1.txt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不仅 add 又 commit 了，就用版本退回恢复到上一次提交状态。</p>
<h2 id="6-文件删除"><a href="#6-文件删除" class="headerlink" title="6.文件删除"></a>6.文件删除</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建 2.txt 文件，add 并 commit 提交到 git 仓库。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># echo "2222222" &gt;&gt; 2.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git add 2.txt</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m " a new 2.txt"</span></div><div class="line">[master 44ec005] a new 2.txt</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面删除它</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># rm -rf 2.txt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git status 可以看到，提示 2.txt 被删除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git status</span></div><div class="line"><span class="comment"># On branch master</span></div><div class="line"><span class="comment"># Changed but not updated:</span></div><div class="line"><span class="comment"># (use "git add/rm &lt;file&gt;..." to update what will be committed)</span></div><div class="line"><span class="comment"># (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># deleted: 2.txt</span></div><div class="line"><span class="comment">#</span></div><div class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果需要恢复就用 git checkout – 2.txt，现在是要从 git 仓库里删除 2.txt，命令 <strong>git rm file</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># git rm 2.txt</span></div><div class="line">rm <span class="string">'2.txt'</span></div><div class="line">[root@localhost gitroot]<span class="comment"># git commit -m "delete 2.txt"</span></div><div class="line">[master b01be1a] delete 2.txt</div><div class="line"> 1 files changed, 0 insertions(+), 1 deletions(-)</div><div class="line"> delete mode 100644 2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就彻底从 git 仓库删除 2.txt 了</p>
<h2 id="7-创建远程仓库（github）"><a href="#7-创建远程仓库（github）" class="headerlink" title="7.创建远程仓库（github）"></a>7.创建远程仓库（github）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先到 <a href="https://github.com/" target="_blank" rel="external">https://github.com/</a> 注册一个账号，创建自己的git，点repositories 再点new</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;名字自定义，比如叫studygit  选择public  点 create repository</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加key：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;右上角点自己头像，选择settings，左侧选择SSH and GPG keys</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;填写好标题，key 在自己主机上创建，命令 ssh-keygen</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># ssh-keygen</span></div><div class="line">Generating public/private rsa key pair.</div><div class="line">Enter file <span class="keyword">in</span> <span class="built_in">which</span> to save the key (/root/.ssh/id_rsa): </div><div class="line">Created directory <span class="string">'/root/.ssh'</span>.</div><div class="line">Enter passphrase (empty <span class="keyword">for</span> no passphrase): </div><div class="line">Enter same passphrase again: </div><div class="line">Your identification has been saved <span class="keyword">in</span> /root/.ssh/id_rsa.</div><div class="line">Your public key has been saved <span class="keyword">in</span> /root/.ssh/id_rsa.pub.</div><div class="line">The key fingerprint is:</div><div class="line">1f:5f:50:04:2b:80:16:16:e1:9a:a8:59:8c:0a:83:f4 root@localhost</div><div class="line">The key<span class="string">'s randomart image is:</span></div><div class="line"><span class="string">+--[ RSA 2048]----+</span></div><div class="line"><span class="string">|      ==.   .oo  |</span></div><div class="line"><span class="string">|     oo  .   o   | </span></div><div class="line"><span class="string">| .   ..   . o    |</span></div><div class="line"><span class="string">|oo.. o     . .   |</span></div><div class="line"><span class="string">|= +Eo   S .   .  |</span></div><div class="line"><span class="string">|o=       . o .   |</span></div><div class="line"><span class="string">|+         . .    |</span></div><div class="line"><span class="string">|                 |</span></div><div class="line"><span class="string">|                 |</span></div><div class="line"><span class="string">+-----------------+</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;右上角点New SSH key，把linux机器上的 /root/.ssh/id_rsa.pub内容粘贴到这里</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost gitroot]<span class="comment"># cat /root/.ssh/id_rsa.pub</span></div><div class="line">ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAmTutzYNSxeZNJmHZXtczs8UbfvPZAkKF/Kwj4f8rlD5IG0Mml+euYjgzb2ppQzvh/sxqFJOKBvM9MhQ5MRc2t4eLm1U11FLyxLLJh6Kdtc6do8lWRJLJFOomkky6DQ4ISN8RinW6GMaT22H41xQ4LozIoA2EbYKELsno6MxbFqfwJlwSgv6WB3oBwDWYaY6nf7c04K2jH/xJwxLD8OyO+SzI15JdQBuZrgAXIvh5sQwwX6spKBKqyeYhTOGXZkkI8FSiY90ZMJrsxwZvbZVMNrpvFnXFN5f8ZHeVM33pTpT05EMLt+FrHixuccP8EJjq4efcwQQcrZJoFJYJZSy5vQ== root@localhost</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把本地仓库推送到远程仓库，命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">git remote add origin git@github.com:hcldirgit/studygit.git</div><div class="line">git push -u origin master</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">[root@localhost home]<span class="comment"># mkdir studygit</span></div><div class="line">[root@localhost home]<span class="comment"># cd studygit</span></div><div class="line">[root@localhost studygit]<span class="comment"># echo "# studygit" &gt;&gt; README.md</span></div><div class="line">[root@localhost studygit]<span class="comment"># git init</span></div><div class="line"> itialized empty Git repository <span class="keyword">in</span> /home/studygit/.git/</div><div class="line">[root@localhost studygit]<span class="comment"># git add README.md</span></div><div class="line">[root@localhost studygit]<span class="comment"># git commit -m "first commit"</span></div><div class="line">[master (root-commit) f3cba66] first commit</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 README.md</div><div class="line">[root@localhost studygit]<span class="comment"># git remote add origin git@github.com:hcldirgit/studygit.git</span></div><div class="line">[root@localhost studygit]<span class="comment"># git push -u origin master </span></div><div class="line">The authenticity of host <span class="string">'github.com (192.30.253.112)'</span> can<span class="string">'t be established.</span></div><div class="line"><span class="string">RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.</span></div><div class="line"><span class="string">Are you sure you want to continue connecting (yes/no)? yes</span></div><div class="line"><span class="string">Warning: Permanently added '</span>github.com,192.30.253.112<span class="string">' (RSA) to the list of known hosts.</span></div><div class="line"><span class="string">Counting objects: 6, done.</span></div><div class="line"><span class="string">Delta compression using up to 4 threads.</span></div><div class="line"><span class="string">Compressing objects: 100% (4/4), done.</span></div><div class="line"><span class="string">Writing objects: 100% (6/6), 3.12 KiB, done.</span></div><div class="line"><span class="string">Total 6 (delta 0), reused 0 (delta 0)</span></div><div class="line"><span class="string">To git@github.com:hcldirgit/studygit.git</span></div><div class="line"><span class="string"> * [new branch] master -&gt; master</span></div><div class="line"><span class="string">Branch master set up to track remote branch master from origin.</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下一次再推送，就可以直接 git push </p>
<h2 id="8-克隆远程仓库"><a href="#8-克隆远程仓库" class="headerlink" title="8.克隆远程仓库"></a>8.克隆远程仓库</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 github 上想把别人的仓库克隆下来</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># cd /home/</span></div><div class="line">[root@localhost home]<span class="comment"># git clone git@github.com:aminglinux/lanmp.git</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /home/lanmp/.git/</div><div class="line">Warning: Permanently added the RSA host key <span class="keyword">for</span> IP address <span class="string">'192.30.253.113'</span> to the list of known hosts.</div><div class="line">remote: Counting objects: 26, <span class="keyword">done</span>.</div><div class="line">remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 26</div><div class="line">Receiving objects: 100% (26/26), 5.46 KiB, <span class="keyword">done</span>.</div><div class="line">Resolving deltas: 100% (4/4), <span class="keyword">done</span>.</div><div class="line">[root@localhost home]<span class="comment"># ls</span></div><div class="line">gitroot lanmp studygit</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它提示，会在当前目录下初始化一个仓库，并创建一个.git的目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Initialized empty Git repository <span class="keyword">in</span> /home/lanmp/.git/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;完成后可以看到一个lanmp 的目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost home]<span class="comment"># ls</span></div><div class="line">gitroot lanmp studygit</div><div class="line">[root@localhost home]<span class="comment"># cd lanmp</span></div><div class="line">[root@localhost lanmp]<span class="comment"># ls</span></div><div class="line">lanmp.sh README.md</div><div class="line">[root@localhost lanmp]<span class="comment"># cat README.md</span></div><div class="line"><span class="comment"># lanmp</span></div><div class="line">lamp/lnmp 一键安装脚本</div><div class="line">author: aming</div><div class="line">version: 0.2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑修改后，使用 git push 再推送到远程服务端。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：如果是克隆的别人的 git 仓库，则无法推送，因为别人没有加我的 key 。</p>
<h2 id="9-使用分支"><a href="#9-使用分支" class="headerlink" title="9.使用分支"></a>9.使用分支</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看分支，命令 <strong>git branch</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git branch</span></div><div class="line">* master</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建分支，命令 <strong>git branch 分支名</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git branch yanyi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;切换到创建分之下，<strong>git checkout 分支名</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git checkout yanyi</span></div><div class="line">Switched to branch <span class="string">'yanyi'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在新分支下，创建文件并提交</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># echo "234qw34\qwerqwer" &gt; 2.txt</span></div><div class="line">[root@localhost studygit]<span class="comment"># git add 2.txt</span></div><div class="line">[root@localhost studygit]<span class="comment"># git commit -m "add new 2.txt"</span></div><div class="line">[yanyi acbc9ea] add new 2.txt</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;切换到 master 分支，查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git checkout master</span></div><div class="line">Switched to branch <span class="string">'master'</span></div><div class="line">[root@localhost studygit]<span class="comment"># ls</span></div><div class="line">lanmp.sh README.md</div></pre></td></tr></table></figure>
<h2 id="10-分支的合并和删除"><a href="#10-分支的合并和删除" class="headerlink" title="10.分支的合并和删除"></a>10.分支的合并和删除</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>把 yanyi 分支合并到 master  ，命令 <code>git merge yanyi</code> 。注意必须先切换到 master 分支下执行命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git checkout master</span></div><div class="line">Switched to branch <span class="string">'master'</span></div><div class="line">[root@localhost studygit]<span class="comment"># git merge yanyi </span></div><div class="line">Updating 23545a4..acbc9ea</div><div class="line">Fast-forward</div><div class="line"> 2.txt | 1 +</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 2.txt</div><div class="line">[root@localhost studygit]<span class="comment"># ls</span></div><div class="line">2.txt lanmp.sh README.md</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果master分支和yanyi分支都对2.txt进行了编辑，当合并时会提示冲突，需要先解决冲突才可以继续合并。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决冲突的方法是在master分支下，编辑2.txt，改为yanyi分支里面2.txt的内容。 然后提交2.txt，再合并yanyi分支。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是这样有一个问题，万一master分支更改的内容是我们想要的呢？ 我们可以编辑2.txt内容，改为我们想要的，然后提交。切换到yanyi分支，然后合并master分支到yanyi分支即可。（倒着合并）合并分支有一个原则，那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>删除分支，命令 <code>git branch -d 分支名</code></strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>如果分支没有合并，删除之前会提示。不合并，强制删除，命令 <code>git branch -D 分支名</code></strong></p>
<h2 id="11-分支使用原则"><a href="#11-分支使用原则" class="headerlink" title="11.分支使用原则"></a>11.分支使用原则</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于分支的应用，建议大家以这样的原则来：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master分支是非常重要的，线上发布代码用这个分支，平时我们开发代码不要在这个分支上。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个dev分支，专门用作开发，只有当发布到线上之前，才会把dev分支合并到master</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开发人员应该在dev的基础上再分支成个人分支，个人分支（在自己pc上）里面开发代码，然后合并到dev分支</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Git%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/01.png?raw=true" alt=""></figure></p>
<h2 id="12-git-stash-保留现场"><a href="#12-git-stash-保留现场" class="headerlink" title="12.git stash 保留现场"></a>12.git stash 保留现场</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当你正在进行项目中某一部分的工作，里面的东西处于一个比较杂乱的状态，而你想转到其他分支上进行一些工作。问题是，你不想提交进行了一半的工作，否则以后你无法回到这个工作点。解决这个问题的办法就是<strong>git stash</strong>命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如我们在 yanyi 分支，编辑了一个新的文件3.txt</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时候我们需要到其他分支去修复一个bug，所以需要先git add 3.txt</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后 git  stash  保存一下现场</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再切换到另外分支去修复bug，修复完bug后，再回到 yanyi 分支</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git stash list</strong> 可以看到我们保存过的现场</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用 <strong>git stash apply</strong> 恢复现场</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以指定stash：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git stash apply stash@{0}</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># echo "wqerqwer\asdasg\q1234234" &gt; 3.txt</span></div><div class="line">[root@localhost studygit]<span class="comment"># ls</span></div><div class="line">2.txt 3.txt lanmp.sh README.md</div><div class="line">[root@localhost studygit]<span class="comment"># git add 3.txt</span></div><div class="line">[root@localhost studygit]<span class="comment"># git stash</span></div><div class="line">Saved working directory and index state WIP on yanyi: acbc9ea add new 2.txt</div><div class="line">HEAD is now at acbc9ea add new 2.txt</div><div class="line">[root@localhost studygit]<span class="comment"># git stash list</span></div><div class="line">stash@&#123;0&#125;: WIP on yanyi: acbc9ea add new 2.txt</div><div class="line">[root@localhost studygit]<span class="comment"># git stash apply stash@&#123;0&#125;</span></div><div class="line"><span class="comment"># On branch yanyi</span></div><div class="line"><span class="comment"># Changes to be committed:</span></div><div class="line"><span class="comment"># (use "git reset HEAD &lt;file&gt;..." to unstage)</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># new file: 3.txt</span></div><div class="line"><span class="comment">#</span></div><div class="line">[root@localhost studygit]<span class="comment"># ls</span></div><div class="line">2.txt 3.txt lanmp.sh README.md</div></pre></td></tr></table></figure>
<h2 id="13-远程分支管理"><a href="#13-远程分支管理" class="headerlink" title="13.远程分支管理"></a>13.远程分支管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看远程库信息，使用<strong>git remote -v</strong> 本地新建的分支如果不推送到远程，对其他人就是不可见的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git remote -v</span></div><div class="line">origin git@github.com:hcldirgit/studygit.git (fetch)</div><div class="line">origin git@github.com:hcldirgit/studygit.git (push)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看远程分支 <strong>git ls-remote origin</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git ls-remote origin</span></div><div class="line">acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 HEAD</div><div class="line">acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/dev</div><div class="line">acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/master</div><div class="line">acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/yanyi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从本地推送分支，使用<strong>git push origin branch-name</strong>，如果推送失败，先用<strong>git pull</strong>抓取远程的新提交</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git push origin yanyi</span></div><div class="line">Everything up-to-date</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git pull</span></div><div class="line">Already up-to-date.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在本地创建和远程分支对应的分支，使用<strong>git checkout -b branch-name origin/branch-name</strong>，本地和远程分支的名称最好一致</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@localhost home]<span class="comment"># rm -rf studygit</span></div><div class="line">[root@localhost home]<span class="comment"># git clone git@github.com:hcldirgit/studygit.git</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /home/studygit/.git/</div><div class="line">remote: Counting objects: 9, <span class="keyword">done</span>.</div><div class="line">remote: Compressing objects: 100% (6/6), <span class="keyword">done</span>.</div><div class="line">remote: Total 9 (delta 0), reused 9 (delta 0), pack-reused 0</div><div class="line">Receiving objects: 100% (9/9), <span class="keyword">done</span>.</div><div class="line">[root@localhost home]<span class="comment"># cd studygit</span></div><div class="line">[root@localhost studygit]<span class="comment"># ls</span></div><div class="line">2.txt lanmp.sh README.md</div><div class="line">[root@localhost studygit]<span class="comment"># git branch</span></div><div class="line">* master</div><div class="line">[root@localhost studygit]<span class="comment"># git checkout -b dev origin/dev</span></div><div class="line">Branch dev <span class="built_in">set</span> up to track remote branch dev from origin.</div><div class="line">Switched to a new branch <span class="string">'dev'</span></div><div class="line">[root@localhost studygit]<span class="comment"># git checkout -b yanyi origin/yanyi</span></div><div class="line">Branch yanyi <span class="built_in">set</span> up to track remote branch yanyi from origin.</div><div class="line">Switched to a new branch <span class="string">'yanyi'</span></div><div class="line">[root@localhost studygit]<span class="comment"># git checkout -b yi origin/yi </span></div><div class="line">Branch yi <span class="built_in">set</span> up to track remote branch yi from origin.</div><div class="line">Switched to a new branch <span class="string">'yi'</span></div><div class="line">[root@localhost studygit]<span class="comment"># git branch</span></div><div class="line"> dev</div><div class="line"> master</div><div class="line"> yanyi</div><div class="line">* yi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从远程抓取分支，使用<strong>git pull</strong>，如果有冲突，要先处理冲突。</p>
<h2 id="14-标签管理"><a href="#14-标签管理" class="headerlink" title="14.标签管理"></a>14.标签管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;标签类似于快照功能，我们可以给版本库打一个标签，记录某个时刻库的状态。我们可以随时恢复到该状态。</p>
<p><strong><code>git checkout master</code> 先切到master分支上</strong></p>
<p><strong><code>git tag v1.0</code>  给master打一个标签v1.0</strong></p>
<p><strong><code>git tag</code> 可以查看所有的标签</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git tag v1.0</span></div><div class="line">[root@localhost studygit]<span class="comment"># git tag</span></div><div class="line">v1.0</div><div class="line">[root@localhost studygit]<span class="comment"># git show v1.0</span></div><div class="line">commit acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date: Mon Jan 9 05:08:10 2017 +0800</div><div class="line"> </div><div class="line"> add new 2.txt</div><div class="line"> </div><div class="line">diff --git a/2.txt b/2.txt</div><div class="line">new file mode 100644</div><div class="line">index 0000000..e9af856</div><div class="line">--- /dev/null</div><div class="line">+++ b/2.txt</div><div class="line">@@ -0,0 +1 @@</div><div class="line">+234qw34\qwerqwer</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>tag是针对commit来打标签的，所以可以针对历史的commit来打tag</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令 <strong>git log –pretty=oneline –abbrev-commit</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git log --pretty=oneline</span></div><div class="line">acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 add new 2.txt</div><div class="line">23545a469bcea58ba25718515d1de2c78c9d7235 add lanmp.sh</div><div class="line">f3cba6670b29b8848c8a8ea61777918f097bcfbe first commit</div><div class="line">[root@localhost studygit]<span class="comment"># git log --pretty=oneline --abbrev-commit</span></div><div class="line">acbc9ea add new 2.txt</div><div class="line">23545a4 add lanmp.sh</div><div class="line">f3cba66 first commit</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git tag v0.9 23545a4</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git tag v0.9 23545a4</span></div><div class="line">[root@localhost studygit]<span class="comment"># git tag</span></div><div class="line">v0.9</div><div class="line">v1.0</div><div class="line">[root@localhost studygit]<span class="comment"># git show v0.9</span></div><div class="line">commit 23545a469bcea58ba25718515d1de2c78c9d7235</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date: Sun Jan 8 04:13:11 2017 +0800</div><div class="line"></div><div class="line">add lanmp.sh</div><div class="line"></div><div class="line">diff --git a/lanmp.sh b/lanmp.sh</div><div class="line">new file mode 100644</div><div class="line">index 0000000..5a1db53</div><div class="line">--- /dev/null</div><div class="line">+++ b/lanmp.sh</div><div class="line">@@ -0,0 +1,465 @@</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git tag -a v0.8 -m “tag just v1.1 and so on”  acbc9ea</strong>  可以对标签进行描述</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git tag -a v0.8 -m "tag just v1.1 and so on" acbc9ea</span></div><div class="line">[root@localhost studygit]<span class="comment"># git tag</span></div><div class="line">v0.8</div><div class="line">v0.9</div><div class="line">v1.0</div><div class="line">[root@localhost studygit]<span class="comment"># git show v0.8</span></div><div class="line">tag v0.8</div><div class="line">Tagger: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date: Mon Jan 9 19:32:17 2017 +0800</div><div class="line"> </div><div class="line">tag just v1.1 and so on</div><div class="line"> </div><div class="line">commit acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1</div><div class="line">Author: yanyi &lt;hcldir@qq.com&gt;</div><div class="line">Date: Mon Jan 9 05:08:10 2017 +0800</div><div class="line"> </div><div class="line"> add new 2.txt</div><div class="line"> </div><div class="line">diff --git a/2.txt b/2.txt</div><div class="line">new file mode 100644</div><div class="line">index 0000000..e9af856</div><div class="line">--- /dev/null</div><div class="line">+++ b/2.txt</div><div class="line">@@ -0,0 +1 @@</div><div class="line">+234qw34\qwerqwer</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git tag -d v0.8</strong> 删除标签</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git tag -d v0.8</span></div><div class="line">Deleted tag <span class="string">'v0.8'</span> (was 2321146)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git push origin v1.0</strong>   推送指定标签到远程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git push origin v1.0 </span></div><div class="line">Total 0 (delta 0), reused 0 (delta 0)</div><div class="line">To git@github.com:hcldirgit/studygit.git</div><div class="line">* [new tag]         v1.0 -&gt; v1.0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git push –tag origin</strong>   推送所有标签</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git push --tag origin</span></div><div class="line">Total 0 (delta 0), reused 0 (delta 0)</div><div class="line">To git@github.com:hcldirgit/studygit.git</div><div class="line"> * [new tag] v0.9 -&gt; v0.9</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果本地删除了一个标签，远程也想要删除需要这样操作：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git tag v1.0 -d</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git push origin :refs/tags/v1.0</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git tag v1.0 -d</span></div><div class="line">Deleted tag <span class="string">'v1.0'</span> (was acbc9ea)</div><div class="line">[root@localhost studygit]<span class="comment"># git push origin :refs/tags/v1.0</span></div><div class="line">To git@github.com:hcldirgit/studygit.git</div><div class="line">- [deleted]          v1.0</div></pre></td></tr></table></figure>
<h2 id="15-git-别名"><a href="#15-git-别名" class="headerlink" title="15.git 别名"></a>15.git 别名</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git commit 这个命令是不是有点长？ 用别名可以提高我们的工作效率</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –global alias.ci commit</strong> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –global alias.co  checkout</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –global alias.br  branch</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git config --global alias.ci comit</span></div><div class="line">[root@localhost studygit]<span class="comment"># git config --global alias.br branch</span></div><div class="line">[root@localhost studygit]<span class="comment"># git config --global alias.co checkout</span></div><div class="line">[root@localhost studygit]<span class="comment"># git config --global alias.lg "log --pretty=oneline"</span></div><div class="line">[root@localhost studygit]<span class="comment"># git br</span></div><div class="line"> dev</div><div class="line">* master</div><div class="line"> yanyi</div><div class="line"> yi</div><div class="line">[root@localhost studygit]<span class="comment"># git co dev</span></div><div class="line">Switched to branch <span class="string">'dev'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;别名所在文件 <strong>/root/.gitconfig</strong> 也可以直接编辑该文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># cat /root/.gitconfig</span></div><div class="line">[user]</div><div class="line">      name = yanyi</div><div class="line">      email = hcldir@qq.com</div><div class="line">[<span class="built_in">alias</span>]</div><div class="line">      ci = comit</div><div class="line">      br = branch</div><div class="line">      co = checkout</div><div class="line">      lg = <span class="built_in">log</span> --pretty=oneline</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看git别名使用命令</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –list |grep alias</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git config --list |grep alias</span></div><div class="line">alias.ci=comit</div><div class="line">alias.br=branch</div><div class="line">alias.co=checkout</div><div class="line">alias.lg=<span class="built_in">log</span> --pretty=oneline</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –list</strong> 可以详细查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git config --list</span></div><div class="line">user.name=yanyi</div><div class="line">user.email=hcldir@qq.com</div><div class="line">alias.ci=comit</div><div class="line">alias.br=branch</div><div class="line">alias.co=checkout</div><div class="line">alias.lg=<span class="built_in">log</span> --pretty=oneline</div><div class="line">core.repositoryformatversion=0</div><div class="line">core.filemode=<span class="literal">true</span></div><div class="line">core.bare=<span class="literal">false</span></div><div class="line">core.logallrefupdates=<span class="literal">true</span></div><div class="line">remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*</div><div class="line">remote.origin.url=git@github.com:hcldirgit/studygit.git</div><div class="line">branch.master.remote=origin</div><div class="line">branch.master.merge=refs/heads/master</div><div class="line">branch.dev.remote=origin</div><div class="line">branch.dev.merge=refs/heads/dev</div><div class="line">branch.yanyi.remote=origin</div><div class="line">branch.yanyi.merge=refs/heads/yanyi</div><div class="line">branch.yi.remote=origin</div><div class="line">branch.yi.merge=refs/heads/yi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>查询log小技巧</strong>：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git config --global alias.lg <span class="string">"log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset' --abbrev-commit"</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset' --abbrev-commit"</span></div><div class="line">[root@localhost studygit]<span class="comment"># git lg</span></div><div class="line">* acbc9ea - (HEAD, origin/yi, origin/yanyi, origin/master, origin/dev, origin/HEAD, yi, yanyi, master, dev) add new 2.txt (15 hours ago) &lt;yanyi&gt;</div><div class="line">* 23545a4 - (v0.9) add lanmp.sh (2 days ago) &lt;yanyi&gt;</div><div class="line">* f3cba66 - first commit (2 days ago) &lt;yanyi&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用以后，git 就会有颜色</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Git%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;取消别名</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>git config –global –unset alias.br</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># git config --global --unset alias.br</span></div><div class="line">[root@localhost studygit]<span class="comment"># git br</span></div><div class="line">git: <span class="string">'br'</span> is not a git <span class="built_in">command</span>. See <span class="string">'git --help'</span>.</div><div class="line"></div><div class="line">Did you mean this?</div><div class="line">        var</div><div class="line">`</div></pre></td></tr></table></figure>
<h2 id="16-搭建-git-服务器"><a href="#16-搭建-git-服务器" class="headerlink" title="16.搭建 git 服务器"></a>16.搭建 git 服务器</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;github 毕竟是公开的，而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的，只自己公司使用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;yum install git安装git</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@gitserver ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@gitserver ~]<span class="comment"># yum install -y git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>useradd -s /usr/bin/git-shell git 添加git用户，并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@gitserver ~]<span class="comment"># useradd -s /usr/bin/git-shell git</span></div><div class="line">[root@gitserver ~]<span class="comment"># cd /home/git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建 authorized_keys 文件，并更改属主、属组和权限，用来存客户端机器上的公钥  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@gitserver git]<span class="comment"># mkdir .ssh</span></div><div class="line">[root@gitserver git]<span class="comment"># touch .ssh/authorized_keys</span></div><div class="line">[root@gitserver git]<span class="comment"># vim .ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;公钥内容在，客户端 /root/.shh/id_rsa.pub</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># cat /root/.ssh/id_rsa.pub</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@gitserver git]<span class="comment"># chown -R git .ssh</span></div><div class="line">[root@gitserver git]<span class="comment"># chmod 700 .ssh</span></div><div class="line">[root@gitserver git]<span class="comment"># chmod 600 .ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看能否连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># ssh git@192.168.0.91</span></div><div class="line">The authenticity of host <span class="string">'192.168.0.91 (192.168.0.91)'</span> can<span class="string">'t be established.</span></div><div class="line"><span class="string">RSA key fingerprint is 38:82:75:f9:11:a3:56:b4:00:ce:46:75:65:7c:5c:9b.</span></div><div class="line"><span class="string">Are you sure you want to continue connecting (yes/no)? yes</span></div><div class="line"><span class="string">Warning: Permanently added '</span>192.168.0.91<span class="string">' (RSA) to the list of known hosts.</span></div><div class="line"><span class="string">fatal: What do you think I am? A shell?</span></div><div class="line"><span class="string">Connection to 192.168.0.91 closed.</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就说明可以连接</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主机设置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@gitserver git]<span class="comment"># usermod -s /bin/bash git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost studygit]<span class="comment"># ssh git@192.168.0.91</span></div><div class="line">Last login: Mon Jan 9 21:53:51 2017 from 192.168.0.92</div><div class="line">[git@gitserver ~]$ <span class="built_in">logout</span></div><div class="line">Connection to 192.168.0.91 closed.</div><div class="line">[root@localhost studygit]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样检测客户端公钥能登录主机，检测完成主机改回设置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@gitserver git]<span class="comment"># usermod -s /usr/bin/git-shell git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样客户端就不能登录主机</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定好存储git仓库的目录，比如 /data/gitroot</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@gitserver git]<span class="comment"># mkdir -p /data/gitroot</span></div><div class="line">[root@gitserver git]<span class="comment"># cd /data/gitroot</span></div><div class="line">[root@gitserver gitroot]<span class="comment"># git init --bare sample.git</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /data/gitroot/sample.git/</div><div class="line">[root@gitserver gitroot]<span class="comment"># ls -la</span></div><div class="line">总用量 12</div><div class="line">drwxr-xr-x 3 root root 4096 1月 9 22:16 .</div><div class="line">drwxr-xr-x 3 root root 4096 1月 9 22:16 ..</div><div class="line">drwxr-xr-x 7 root root 4096 1月 9 22:16 sample.git</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：<strong>git init –bare sample.git</strong>  会创建一个裸仓库，裸仓库没有工作区，因为服务器上的Git仓库纯粹是为了共享，所以不让用户直接登录到服务器上去改工作区，并且服务器上的Git仓库通常都以.git结尾</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@gitserver gitroot]<span class="comment"># chown -R git.git sample.git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在客户端上（自己pc）克隆远程仓库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost home]<span class="comment"># git clone git@192.168.0.91:/data/gitroot/sample.git</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /home/sample/.git/</div><div class="line">warning: You appear to have cloned an empty repository.</div><div class="line">[root@localhost home]<span class="comment"># ls</span></div><div class="line">gitroot lanmp sample studygit</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端操作，推送到服务器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@localhost sample]<span class="comment"># ls</span></div><div class="line">[root@localhost sample]<span class="comment"># echo "1234\abcd\5678\efgh" &gt; 1.txt</span></div><div class="line">[root@localhost sample]<span class="comment"># git add 1.txt</span></div><div class="line">[root@localhost sample]<span class="comment"># git commit -m "add new 1.txt"</span></div><div class="line">[master (root-commit) a86f429] add new 1.txt</div><div class="line"> 1 files changed, 1 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 1.txt</div><div class="line">[root@localhost sample]<span class="comment"># git push -u origin master</span></div><div class="line">Counting objects: 3, <span class="keyword">done</span>.</div><div class="line">Writing objects: 100% (3/3), 224 bytes, <span class="keyword">done</span>.</div><div class="line">Total 3 (delta 0), reused 0 (delta 0)</div><div class="line">To git@192.168.0.91:/data/gitroot/sample.git</div><div class="line"> * [new branch] master -&gt; master</div><div class="line">Branch master <span class="built_in">set</span> up to track remote branch master from origin.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上操作是在git服务器上做的，平时git服务器是不需要开发人员登录修改代码的，它仅仅是充当着一个服务器的角色，就像github一样，平时操作都是在我们自己的pc上做的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里 git clone git@ip:/data/gitroot/sample.git</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时就可以在当前目录下生成一个sample的目录，这个就是我们克隆的远程仓库了。进入到这里面，可以开发一些代码，然后push到远程。  </p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/FTP/4. vsftp 利用mysql 验证虚拟账户/">vsftp 利用mysql 验证虚拟账户</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/FTP/">FTP</a>
			</span>
		
	</div>

	

	
		<h2 id="安装vsftpd"><a href="#安装vsftpd" class="headerlink" title="安装vsftpd"></a>安装vsftpd</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y  vsftpd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑vsftpd.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">listen=YES</div><div class="line">connect_from_port_20=YES</div><div class="line">pasv_enable=YES</div><div class="line">tcp_wrappers=YES</div><div class="line">local_enable=YES</div><div class="line">chroot_local_user=yes</div><div class="line">anonymous_enable=NO</div><div class="line">guest_enable=YES</div><div class="line">guest_username=vsftpdguest</div><div class="line">user_config_dir=/etc/vsftpd/vsftpd_user_conf</div><div class="line">pam_service_name=/etc/pam.d/vsftpd</div><div class="line">dirmessage_enable=YES</div><div class="line">idle_session_timeout=600</div><div class="line">check_shell=NO</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个虚拟用户映射系统用户    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">useradd –s /sbin/nologin vsftpdguest</div></pre></td></tr></table></figure>
<h2 id="安装-mysql"><a href="#安装-mysql" class="headerlink" title="安装 mysql"></a>安装 mysql</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具体步骤参考 <a href="https://hcldirgit.github.io/2017/07/29/1.%20Mysql%E5%AE%89%E8%A3%85/" target="_blank" rel="external">MySQL安装</a></p>
<h2 id="安装-pam-mysql"><a href="#安装-pam-mysql" class="headerlink" title="安装 pam-mysql"></a>安装 pam-mysql</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">wget http://cdnetworks-kr-1.dl.sourceforge.net/project/pam-mysql/pam-mysql/0.7RC1/pam_mysql-0.7RC1.tar.gz</div><div class="line">tar zxvf  pam_mysql-0.7RC1.tar.gz</div><div class="line"><span class="built_in">cd</span> pam_mysql-0.7RC1</div><div class="line">./configure --with-mysql=/usr/<span class="built_in">local</span>/mysql --with-pam=/usr --with-pam-mods-dir=/usr/lib</div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<h2 id="创建vsftp-库和相关的表并授权"><a href="#创建vsftp-库和相关的表并授权" class="headerlink" title="创建vsftp 库和相关的表并授权"></a>创建vsftp 库和相关的表并授权</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&gt;create database vsftp;</div><div class="line">&gt;use vsftp ;</div><div class="line">&gt;create table users ( name char(16) binary ,passwd char(125) binary ) ;</div><div class="line">&gt;insert into users (name,passwd) values (<span class="string">'test001'</span>,password(<span class="string">'123456'</span>));</div><div class="line">&gt;insert into users (name,passwd) values (<span class="string">'test002'</span>,password(<span class="string">'234567'</span>));</div><div class="line">&gt;grant select on vsftp.users to vsftpdguest@localhost identified by <span class="string">'vsftpdguest'</span>;</div></pre></td></tr></table></figure>
<h2 id="创建虚拟账户的配置文件"><a href="#创建虚拟账户的配置文件" class="headerlink" title="创建虚拟账户的配置文件"></a>创建虚拟账户的配置文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mkdir /etc/vsftpd/vsftpd_user_conf </div><div class="line"><span class="built_in">cd</span>  /etc/vsftpd/vsftpd_user_conf</div><div class="line">vim test001</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">local_root=/ftp/        </div><div class="line">write_enable=YES</div><div class="line">virtual_use_local_privs=YES</div><div class="line">chmod_enable=YES</div></pre></td></tr></table></figure>
<h2 id="编辑验证文件"><a href="#编辑验证文件" class="headerlink" title="编辑验证文件"></a>编辑验证文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim  /etc/pam.d/vsftpd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#%PAM-1.0</span></div><div class="line">auth required /usr/lib/pam_mysql.so user=vsftpdguest passwd=vsftpdguest host=localhost db=vsftp table=users usercolumn=name passwdcolumn=passwd crypt=2</div><div class="line">account required /usr/lib/pam_mysql.so user=vsftpdguest passwd=vsftpdguest host=localhost db=vsftp table=users usercolumn=name passwdcolumn=passwd crypt=2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不想使用mysql也可以使用文件的形式来搞虚拟账号，请参考<a href="https://hcldirgit.github.io/2017/08/02/2.%20vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/" target="_blank" rel="external">vsftp部署和优化</a></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/FTP/3. ftp的主动模式与被动模式的区别是什么？/">ftp的主动模式与被动模式的区别是什么？</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/FTP/">FTP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FTP协议有两种工作方式：PORT方式和PASV方式，中文意思为主动模式和被动模式</p>
<h2 id="一、什么是主动FTP"><a href="#一、什么是主动FTP" class="headerlink" title="一、什么是主动FTP"></a>一、什么是主动FTP</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主动模式的FTP工作原理：客户端从一个任意的非特权端口N连接到FTP服务器的命令端口，也就是21端口。然后客户端开始监听端口N+1，并发送FTP命令“port N+1”到FTP服务器。接着服务器会从它自己的数据端口（20）连接到客户端指定的数据端口（N+1）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 针对FTP服务器前面的防火墙来说，必须允许以下通讯才能支持主动方式FTP：    </p>
<ol>
<li><p>任何大于1024的端口到FTP服务器的21端口。（客户端初始化的连接）  </p>
</li>
<li><p>FTP服务器的21端口到大于1024的端口。 （服务器响应客户端的控制端口） </p>
</li>
<li><p>FTP服务器的20端口到大于1024的端口。（服务器端初始化数据连接到客户端的数据端口）</p>
</li>
<li><p>大于1024端口到FTP服务器的20端口（客户端发送ACK响应到服务器的数据端口） </p>
</li>
</ol>
<h2 id="二、什么是被动FTP"><a href="#二、什么是被动FTP" class="headerlink" title="二、什么是被动FTP"></a>二、什么是被动FTP</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了解决服务器发起到客户的连接的问题，人们开发了一种不同的FTP连接方式。这就是所谓的被动方式，或者叫做PASV，当客户端通知服务器它处于被动模式时才启用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在被动方式FTP中，命令连接和数据连接都由客户端发起，这样就可以解决从服务器到客户端的数据端口的入方向连接被防火墙过滤掉的问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当开启一个 FTP连接时，客户端打开两个任意的非特权本地端口（N &gt; 1024和N+1）。第一个端口连接服务器的21端口，但与主动方式的FTP不同，客户端不会提交PORT命令并允许服务器来回连它的数据端口，而是提交 PASV命令。这样做的结果是服务器会开启一个任意的非特权端口（P &gt; 1024），并发送PORT P命令给客户端。然后客户端发起从本地端口N+1到服务器的端口P的连接用来传送数据。  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于服务器端的防火墙来说，必须允许下面的通讯才能支持被动方式的FTP:     </p>
<ol>
<li><p>从任何大于1024的端口到服务器的21端口（客户端初始化的连接）  </p>
</li>
<li><p>服务器的21端口到任何大于1024的端口（服务器响应到客户端的控制端口的连接） </p>
</li>
<li><p>从任何大于1024端口到服务器的大于1024端口（客户端初始化数据连接到服务器指定的任意端口）</p>
</li>
<li><p>服务器的大于1024端口到远程的大于1024的端口（服务器发送ACK响应和数据到客户端的数据端口）</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上面可以看出，两种方式的命令链路连接方法是一样的，而数据链路的建立方法就完全不同，如下图：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FTP服务器的主动工作模式</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/ftp%E7%9A%84%E4%B8%BB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E4%B8%8E%E8%A2%AB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%8C%BA%E5%88%AB%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F/01.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FTP服务器的被动工作模式</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/ftp%E7%9A%84%E4%B8%BB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E4%B8%8E%E8%A2%AB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%8C%BA%E5%88%AB%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F/02.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上关于主动和被动FTP的解释，可以简单概括为以下两点：</p>
<p>1.主动FTP：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令连接：客户端 &gt;1024端口 -&gt; 服务器 21端口</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据连接：客户端 &gt;1024端口 &lt;- 服务器 20端口</p>
<ol>
<li>被动FTP：</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令连接：客户端 &gt;1024端口 -&gt; 服务器 21端口 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据连接：客户端 &gt;1024端口 -&gt; 服务器 &gt;1024端口</p>
<h2 id="三、主动模式ftp与被动模式FTP优点和缺点："><a href="#三、主动模式ftp与被动模式FTP优点和缺点：" class="headerlink" title="三、主动模式ftp与被动模式FTP优点和缺点："></a>三、主动模式ftp与被动模式FTP优点和缺点：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主动FTP对FTP服务器的管理和安全很有利，但对客户端的管理不利。因为FTP服务器企图与客户端的高位随机端口建立连接，而这个端口很有可能被客户端的防火墙阻塞掉。被动FTP对FTP客户端的管理有利，但对服务器端的管理不利。因为客户端要与服务器端建立两个连接，其中一个连到一个高位随机端口，而这个端口很有可能被服务器端的防火墙阻塞掉。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/ftp%E7%9A%84%E4%B8%BB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E4%B8%8E%E8%A2%AB%E5%8A%A8%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%8C%BA%E5%88%AB%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F/03.jpg?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/FTP/2. vsftp部署和优化/">vsftp部署和优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/FTP/">FTP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;作为系统默认自带的 ftp 服务软件，vsftp 也是比较常用的。</p>
<h2 id="1-安装-vsftp"><a href="#1-安装-vsftp" class="headerlink" title="1.安装 vsftp"></a>1.安装 vsftp</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y vsftpd db4-utils</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里安装两个软件包，同时会把依赖的包安装上。其中 db4-utils 用来生成木马库文件。</p>
<h2 id="2-建立帐号"><a href="#2-建立帐号" class="headerlink" title="2.建立帐号"></a>2.建立帐号</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vsftp 默认是可以支持使用系统帐号体系登录的，但这样不太安全，所以建议使用虚拟账号体系。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;建立虚拟账号相关的系统帐号</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># useradd virftp -s /sbin/nologin</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;建立虚拟账户相关的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># vim /etc/vsftpd/vsftpd_login</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">test1</div><div class="line">123456</div><div class="line">test2</div><div class="line">abcdef</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改该文件的权限，提升安全级别</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># chmod 600 /etc/vsftpd/vsftpd_login</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vsftpd 使用的密码文件肯定不是铭文的，需要生成对应的库文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;建立虚拟帐号相关的目录以及配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /etc/vsftpd/vsftpd_user_conf</span></div><div class="line">[root@192 ~]<span class="comment"># cd /etc/vsftpd/vsftpd_user_conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3.创建和用户对应的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># vim test1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">local_root=/home/virftp/test1</div><div class="line">anonymous_enable=NO</div><div class="line">write_enable=YES</div><div class="line">local_umask=022</div><div class="line">anon_upload_enable=NO</div><div class="line">anon_mkdir_write_enable=NO</div><div class="line">idle_session_timeout=600</div><div class="line">data_connection_timeout=120</div><div class="line">max_clients=10</div><div class="line">max_per_ip=5</div><div class="line">local_max_rate=50000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明:local_root 为该帐号的家目录，anonymous_enable 用来限制是否允许匿名帐号登录，若为 NO 表示不允许匿名帐号登录， write_enable=YES 表示可写，local_umask 指定 umask 值，anon_upload_enable 是否允许匿名帐号上传文件，anon_mkdir_write_enable 是否允许匿名帐号可写。以上为关键参数，其他暂时不用关心，创建 test2 帐号的步骤和 test1 一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># mkdir /home/virftp/test1</span></div><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># chown -R virftp:virftp /home/virftp</span></div><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># vim /etc/pam.d/vsftpd</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在最开头添加两行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login</div><div class="line">account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：linux 为 64 位系统，所以库文件为 /lib64/security/pam_userdb.so ，若系统为 32位，那么库文件在 /lib/security/pam_userdb.so</p>
<h2 id="4-修改全局配置文件-etc-vsftpd-conf"><a href="#4-修改全局配置文件-etc-vsftpd-conf" class="headerlink" title="4.修改全局配置文件 /etc/vsftpd.conf"></a>4.修改全局配置文件 /etc/vsftpd.conf</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># vim /etc/vsftpd/vsftpd.conf</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/04.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/vsftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再增加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">chroot_local_user=YES</div><div class="line">guest_enabled=YES</div><div class="line">guest_username=virftp</div><div class="line">virtual_use_local_privs=YES</div><div class="line">user_config_dir=/etc/vsftpd/vsftpd_user_conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后启动 vsftpd 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 vsftpd_user_conf]<span class="comment"># /etc/init.d/vsftpd start </span></div><div class="line">为 vsftpd 启动 vsftpd： [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果服务启动不了，很有可能是前面 21端口占用。测试过程和 pure-ftpd 一样，如果用户无法登录，查看日志 /var/log/srcure</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/FTP/1. pureftp部署和优化/">pureftp部署和优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/FTP/">FTP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FTP 是 File Transfer Protocol （文件传输协议）的英文简称，而中文简称为“文传协议”用于 Internet 上的控制文件的双向传输。同时，它也是一个应用程序（Applocation）用户可以通过它把自己的 PC 机与世界各地所有运行 FTP 协议的服务器相连，访问服务器上的大量程序和信息。 FTP 的主要作用，就是让用户连接上一个远程计算机（这些计算机上运行着 FTP 服务器程序）查看远程计算机有哪些文件，然后把文件从远程计算机上拷贝到本地计算机。或把本地计算机的文件送到远程计算机去。FTP 用得比 NFS 更多。</p>
<h2 id="安装pure-ftpd"><a href="#安装pure-ftpd" class="headerlink" title="安装pure-ftpd"></a>安装pure-ftpd</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 centos 或者 redhat linux 上有自带的 ftp 软件叫做 vsftp ，但 pure-ftpd 比 vsftp 配置起来更加灵活。</p>
<h3 id="1-下载软件"><a href="#1-下载软件" class="headerlink" title="1.下载软件"></a>1.下载软件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://www.pureftpd.org/project/pure-ftpd" target="_blank" rel="external">pure-ftpd 官网</a> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># cd /usr/local/src</span></div><div class="line">[root@192 src]<span class="comment"># wget http://download.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.42.tar.bz2</span></div></pre></td></tr></table></figure>
<h3 id="2-安装-pure-ftpd"><a href="#2-安装-pure-ftpd" class="headerlink" title="2.安装 pure-ftpd"></a>2.安装 pure-ftpd</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># tar jxvf pure-ftpd-1.0.42.tar.bz2 </span></div><div class="line">[root@192 src]<span class="comment"># cd pure-ftpd-1.0.42</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">./configure \</div><div class="line">--prefix=/usr/<span class="built_in">local</span>/pureftpd \</div><div class="line">--without-inetd \</div><div class="line">--with-altlog \</div><div class="line">--with-puredb \</div><div class="line">--with-throttling \</div><div class="line">--with-peruserlimits \</div><div class="line">--with-tls</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">configure: error: <span class="keyword">in</span> `/usr/<span class="built_in">local</span>/src/pure-ftpd-1.0.42<span class="string">':</span></div><div class="line"><span class="string">configure: error: no acceptable C compiler found in $PATH</span></div><div class="line"><span class="string">See `config.log'</span> <span class="keyword">for</span> more details</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y gcc</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译报错：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">configure: error: OpenSSL headers not found.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y openssl-devel</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/pureftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/01.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 pure-ftpd-1.0.42]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<h3 id="3-配置-pure-ftpd"><a href="#3-配置-pure-ftpd" class="headerlink" title="3.配置 pure-ftpd"></a>3.配置 pure-ftpd</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@192 pure-ftpd-1.0.42]<span class="comment"># cd configuration-file/</span></div><div class="line">[root@192 configuration-file]<span class="comment"># mkdir -p /usr/local/pureftpd/etc/</span></div><div class="line">[root@192 configuration-file]<span class="comment"># cp pure-ftpd.conf /usr/local/pureftpd/etc/pure-ftpd.conf</span></div><div class="line">[root@192 configuration-file]<span class="comment"># cp pure-config.pl /usr/local/pureftpd/sbin/pure-config.pl</span></div><div class="line">[root@192 configuration-file]<span class="comment"># chmod 755 /usr/local/pureftpd/sbin/pure-config.pl</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在启动 pure-ftpd 之前需要先修改配置文件，配置文件为 /usr/local/pureftpd/etc/pure-ftpd.conf，把自带配置删除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 configuration-file]<span class="comment"># &gt; /usr/local/pureftpd/etc/pure-ftpd.conf </span></div><div class="line">[root@192 configuration-file]<span class="comment"># vim /usr/local/pureftpd/etc/pure-ftpd.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">ChrootEveryone             yes</div><div class="line">BrokenClientsCompatibility no</div><div class="line">MaxClientsNumber           50</div><div class="line">Daemonize                  yes</div><div class="line">MaxClientsPerIP            8</div><div class="line">VerboseLog                 no</div><div class="line">DisplayDotFiles            yes</div><div class="line">AnonymousOnly              no</div><div class="line">NoAnonymous                no</div><div class="line">SyslogFacility             ftp</div><div class="line">DontResolve                yes</div><div class="line">MaxIdleTime                15</div><div class="line">PureDB                     /usr/<span class="built_in">local</span>/pureftpd/etc/pureftpd.pdb</div><div class="line">LimitRecursion             3136 8</div><div class="line">AnonymousCanCreateDirs     no</div><div class="line">MaxLoad                    4</div><div class="line">AntiWarez                  yes</div><div class="line">Umask                      133:022</div><div class="line">MinUID                     100</div><div class="line">AllowUserFXP               no</div><div class="line">AllowAnonymousFXP          no</div><div class="line">ProhibitDotFilesWrite      no</div><div class="line">ProhibitDotFilesRead       no</div><div class="line">AutoRename                 no</div><div class="line">AnonymousCantUpload        no</div><div class="line">PIDFile                    /usr/<span class="built_in">local</span>/pureftpd/var/run/pure-ftpd.pid</div><div class="line">MaxDiskUsage               99</div><div class="line">CustomerProof              yes</div></pre></td></tr></table></figure>
<h3 id="4-启动-pure-ftpd"><a href="#4-启动-pure-ftpd" class="headerlink" title="4.启动 pure-ftpd"></a>4.启动 pure-ftpd</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 configuration-file]<span class="comment"># cd /usr/local/pureftpd/</span></div><div class="line">[root@192 pureftpd]<span class="comment"># ./sbin/pure-config.pl ./etc/pure-ftpd.conf</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/pureftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果启动成功，会显示一行长长的以 Running 开头的信息，否则那就是错误信息。重启会比较麻烦一些，重启可以使用下面的命令来实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 pureftpd]<span class="comment"># killall pure-ftpd</span></div><div class="line">[root@192 pureftpd]<span class="comment"># cd /usr/local/pureftpd/;./sbin/pure-config.pl ./etc/pure-ftpd.conf</span></div></pre></td></tr></table></figure>
<h3 id="5-建立帐号"><a href="#5-建立帐号" class="headerlink" title="5.建立帐号"></a>5.建立帐号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pure-ftpd 使用的帐号并非 linux 系统帐号，而是虚拟帐号。因为这样做比较安全。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir -p /data/www/</span></div><div class="line">[root@192 ~]<span class="comment"># useradd www</span></div><div class="line">[root@192 ~]<span class="comment"># chown -R www:www /data/www/</span></div><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw useradd ftp_user1 -uwww -d /data/www/</span></div><div class="line">Password: </div><div class="line">Enter it again:</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中，-u 将虚拟用户 ftp_user1 与系统用户 www 关联在一起，也就是说使用 ftp_user1 帐号登录 ftp 后，会以 www 的身份来读取文件或下载文件。 -d 后边的目录为 ftp_user1 账户的家目录，这样可以使 ftp_user1 只能访问其家目录 /data/www/ 。到这里还有最关键的一部，就是创建用户信息数据库文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw mkdb</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pure-pw 还可以列出当前的 ftp 帐号，当然也可以删除某个帐号。再创建一个帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw useradd ftp_user2 -uwww -d /tmp/</span></div><div class="line">Password: </div><div class="line">Enter it again: </div><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw mkdb</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列出当前帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw list</span></div><div class="line">ftp_user1 /data/www/./ </div><div class="line">ftp_user2 /tmp/./</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除帐号的命令为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /usr/local/pureftpd/bin/pure-pw userdel ftp_user2</span></div></pre></td></tr></table></figure>
<h3 id="6-测试-pure-ftpd"><a href="#6-测试-pure-ftpd" class="headerlink" title="6.测试 pure-ftpd"></a>6.测试 pure-ftpd</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试需要的工具叫做 lftp ，先安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y lftp</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># touch /data/www/123.txt</span></div><div class="line">[root@192 ~]<span class="comment"># lftp ftp_user1@127.0.0.1</span></div></pre></td></tr></table></figure>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：登录以后可以用 get 下载，put 上传。也可以用 ftp + ip 登录。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/pureftp%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;登录后，使用 ls 命令可以列出当前目录都有什么软件。使用 lftp 工具是为了方便在 linux 系统里测试。最好的测试方法是，在 windows 机器里安装个 ftp 客户端软件（推荐 filezilla-client），然后去远程连接测试。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面使用 yum 安装 pure-ftpd 的方法。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y epel-release expect</span></div><div class="line">[root@192 ~]<span class="comment"># yum install -y pure-ftpd</span></div><div class="line">[root@192 ~]<span class="comment"># curl http://www.apelearn.com/study_v2/.pf.c &gt; /etc/pure-ftpd/pure-ftpd.conf</span></div><div class="line">[root@192 ~]<span class="comment"># user='mkpasswd -l 5 -s 0 -d 0 -C 0'</span></div><div class="line">[root@192 ~]<span class="comment"># pass='mkpasswd -s 0'</span></div><div class="line">[root@192 ~]<span class="comment"># echo $user $pass &gt; /tmp/ftp.pass</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建 ftp 服务的目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir -p /data/ftp</span></div><div class="line">[root@192 ~]<span class="comment"># echo -e "$pass\n$pass"|pure-pw useradd $user -u apache -d /data/ftp/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建密码文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pure-pw mkdb</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列出用户</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pure-pw list</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除帐号</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pure-pw userdel $user</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/pure-ftpd start</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Docker/1. Docker/">Docker</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Docker/">Docker</a>
			</span>
		
	</div>

	

	
		<h2 id="1-Docker简介"><a href="#1-Docker简介" class="headerlink" title="1.Docker简介"></a>1.Docker简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器虚拟化，比传统的虚拟化轻量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2013年出现，发展非常迅猛</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redhat在6.5版本开始支持docker</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用go语言开发，基于apache2.0协议</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开源软件，项目代码在github维护</p>
<h3 id="容器虚拟化和传统虚拟化比较"><a href="#容器虚拟化和传统虚拟化比较" class="headerlink" title="容器虚拟化和传统虚拟化比较"></a>容器虚拟化和传统虚拟化比较</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<h3 id="Docker的优势"><a href="#Docker的优势" class="headerlink" title="Docker的优势"></a>Docker的优势</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动非常快，秒级实现</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;资源利用率很高，一台机器可以跑上千个docker容器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更快的交付和部署，一次创建和配置后，可以在任意地方运行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内核级别的虚拟化，不需要额外的hypevisor支持，会有更高的性能和效率</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;易迁移，平台依赖性不强</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<h3 id="Docker核心概念"><a href="#Docker核心概念" class="headerlink" title="Docker核心概念"></a>Docker核心概念</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;镜像，是一个只读的模板，类似于安装系统用到的那个iso文件，我们通过镜像来完成各种应用的部署。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器，镜像类似于操作系统，而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作，每个容器都是相互隔离的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;仓库，存放镜像的一个场所，仓库分为公开仓库和私有仓库。 最大的公开仓库是Docker hub（hub.docker.com），国内公开仓库（dockerpool.com）</p>
<h2 id="2-Docker安装"><a href="#2-Docker安装" class="headerlink" title="2.Docker安装"></a>2.Docker安装</h2><h3 id="centos6-上安装"><a href="#centos6-上安装" class="headerlink" title="centos6 上安装"></a>centos6 上安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@docker ~]<span class="comment"># yum install -y docker-io</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># /etc/init.d/docker start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如status 查看状态</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># yum install device-mapper-event-libs</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 docker 正常运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># service docker restart</span></div><div class="line">停止 docker： [失败]</div><div class="line">Starting docker: [确定]</div><div class="line">[root@docker ~]<span class="comment"># service docker status</span></div><div class="line">docker (pid 24216) 正在运行...</div></pre></td></tr></table></figure>
<h3 id="centos7-上安装"><a href="#centos7-上安装" class="headerlink" title="centos7 上安装"></a>centos7 上安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># yum install -y docker</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># systemctl start docker</span></div></pre></td></tr></table></figure>
<h2 id="3-Docker-镜像管理"><a href="#3-Docker-镜像管理" class="headerlink" title="3.Docker 镜像管理"></a>3.Docker 镜像管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从docker.com获取centos镜像 docker pull centos</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker pull centos </span></div><div class="line">Using default tag: latest</div><div class="line">Trying to pull repository docker.io/library/centos ... </div><div class="line">latest: Pulling from docker.io/library/centos</div><div class="line">45a2e645736c: Pull complete </div><div class="line">Digest: sha256:c577af3197aacedf79c5a204cd7f493c8e07ffbce7f88f7600bf19c688c38799</div><div class="line">Status: Downloaded newer image <span class="keyword">for</span> docker.io/centos:latest</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker images</strong> 查看本地都有哪些镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY       TAG    IMAGE ID     CREATED       SIZE</div><div class="line">docker.io/centos latest 67591570dd29 5 weeks ago   191.8 MB</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker tag centos yanyi</strong> 为centos镜像设置标签为yanyi</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker tag centos yanyi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再使用docker images查看会多出来一行，改行的image id和centos的一样</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY       TAG      IMAGE ID     CREATED     SIZE</div><div class="line">docker.io/centos latest 67591570dd29 5 weeks ago 191.8 MB</div><div class="line">yanyi            latest 67591570dd29 5 weeks ago 191.8 MB</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker search [image-name]</strong> 从docker仓库搜索docker镜像，后面是关键词</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker search centos</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -t -i centos /bin/bash</strong> 用下载到的镜像开启容器，<strong>-i</strong>表示让容器的标准输入打开，<strong>-t</strong>表示分配一个伪终端，要把-i -t 放到镜像名字前面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -t -i centos /bin/bash</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当该镜像发生修改后，我们可以把该镜像提交重新生成一个新版本进行在本地。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker ps</strong> 查看运行的容器，加上<strong>-a</strong>选项可以查看没有运行的容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps -a</span></div><div class="line">CONTAINER ID IMAGE   COMMAND     CREATED       STATUS                         PORTS NAMES</div><div class="line">6e35c7590c0e centos  <span class="string">"/bin/bash"</span> 2 minutes ago Exited (0) About a minute ago        desperate_jepsen</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker rmi centos</strong> 用来删除指定镜像， 其中后面的参数可以是tag，如果是tag时，实际上是删除该tag，只要该镜像还有其他tag，就不会删除该镜像。当后面的参数为镜像ID时，则会彻底删除整个镜像，连通所有标签一同删除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker rmi yanyi</span></div><div class="line">Untagged: yanyi:latest</div><div class="line">Untagged: docker.io/centos@sha256:c577af3197aacedf79c5a204cd7f493c8e07ffbce7f88f7600bf19c688c38799</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tag 是可以更改的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY       TAG    IMAGE ID     CREATED     SIZE</div><div class="line">docker.io/centos latest 67591570dd29 5 weeks ago 191.8 MB</div><div class="line">[root@docker ~]<span class="comment"># docker tag centos centos:yanyi</span></div><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY       TAG    IMAGE ID     CREATED     SIZE</div><div class="line">centos           yanyi   67591570dd29 5 weeks ago 191.8 MB</div><div class="line">docker.io/centos latest 67591570dd29 5 weeks ago 191.8 MB</div></pre></td></tr></table></figure>
<h2 id="4-docker-镜像使用容器生成新的镜像"><a href="#4-docker-镜像使用容器生成新的镜像" class="headerlink" title="4.docker 镜像使用容器生成新的镜像"></a>4.docker 镜像使用容器生成新的镜像</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker ps</strong> 可以查看运行的镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明现在没有运行的镜像，用 <strong>docker ps -a</strong> 查看没有运行的镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps -a</span></div><div class="line">CONTAINER ID  IMAGE   COMMAND     CREATED      STATUS                   PORTS  NAMES</div><div class="line">6e35c7590c0e  centos  <span class="string">"/bin/bash"</span> 36 hours ago Exited (0) 36 hours ago         desperate_jepsen</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;运行没有运行的镜像，命令 <strong>docker start <container id=""></container></strong> CONTAINER ID可以只输入前3位</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker start 6e3</span></div><div class="line">6e3</div><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID  IMAGE   COMMAND      CREATED      STATUS        PORTS   NAMES</div><div class="line">6e35c7590c0e  entos  <span class="string">"/bin/bash"</span>  36 hours ago  Up 9 seconds            desperate_jepsen</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入运行镜像，命令 <strong>docker exec -it <container id=""> /bin/bash</container></strong> <container id="">也可以只输入前3位</container></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]# docker exec -i -t 6e3 /bin/bash</div><div class="line">[root@6e35c7590c0e /]#</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在镜像中可以执行一些命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@914ff4a5efee /]<span class="comment"># yum install -y net-tools wget</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以查看镜像版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@914ff4a5efee /]<span class="comment"># cat /etc/redhat-release </span></div><div class="line">CentOS Linux release 7.3.1611 (Core)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@914ff4a5efee /]<span class="comment"># uname -a</span></div><div class="line">Linux 914ff4a5efee 3.10.0-514.el7.x86_64 <span class="comment">#1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux</span></div></pre></td></tr></table></figure>
<h3 id="Docker-基于已有镜像的容器创建镜像"><a href="#Docker-基于已有镜像的容器创建镜像" class="headerlink" title="Docker 基于已有镜像的容器创建镜像"></a>Docker 基于已有镜像的容器创建镜像</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;运行docker run后，进入到该容器中，我们做一些变更，比如安装一些东西，然后针对这个容器进行创建新的镜像</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker commit -m “change somth”  -a “somebody info”  container_id （通过docker ps -a获取id） 新镜像名字</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker commit -m "centos_with_nettools_and_wget" -a "yanyi" 914 centos_with_net</span></div><div class="line">sha256:230c1f5d9646084a345b40f75d5eca5d62f4bf7cde8ba03d26a5b161057da55b</div><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY      TAG    IMAGE ID     CREATED        SIZE</div><div class="line">centos_with_net latest 230c1f5d9646 14 seconds ago 262.7 MB</div><div class="line">centos          latest 67591570dd29 5 weeks ago    191.8 MB</div><div class="line">centos          yanyi  67591570dd29 5 weeks ago    191.8 MB</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令有点像svn的提交，-m 加一些改动信息，-a 指定作者相关信息  914这一串为容器id，再后面为新镜像的名字</p>
<h2 id="5-Docker-基于本地模板导入创建镜像"><a href="#5-Docker-基于本地模板导入创建镜像" class="headerlink" title="5.Docker 基于本地模板导入创建镜像"></a>5.Docker 基于本地模板导入创建镜像</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;模块获取，可以直接在网上下载一个模块<a href="https://openvz.org/Download/template/precreated" target="_blank" rel="external">https://openvz.org/Download/template/precreated</a> 可惜速度并不快，若下载了一个centos的模板 centos-6-x86_64-minimal.tar.gz 那么导入该镜像的命令为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># cat centos-6-x86_64-minimal.tar.gz |docker import - centos-6-x86_64</span></div><div class="line">sha256:601270b7e8af4f738fc5d22c0c6b9aa653ebfa917e9a5dcc371a894ed17cb7a1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把现有镜像，导出为一个文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker save -o centos_with_net.tar 230c1f5d9646</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以用该文件恢复本地镜像：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker load --input centos_with_net.tar</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker load &lt; centos_with_net.tar</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker push image_name</strong> 可以把自己的镜像传到dockerhub官方网站上去，但前提是需要先注册一个用户</p>
<h2 id="6-Docker-容器管理"><a href="#6-Docker-容器管理" class="headerlink" title="6.Docker 容器管理"></a>6.Docker 容器管理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker create -it centos</strong> 这样可以创建一个容器，但该容器并没有启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker create -i -t centos</span></div><div class="line">15cb91256d663c18dea652145b5e970220d0295879bbd8647db276eff36a1a76</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker start container_id</strong> 启动容器后，可以使用 docker ps  查看到，有start 就有stop，和restart</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker start 15cb91256d66</span></div><div class="line">15cb91256d66</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之前我们使用的<strong>docker run</strong> 相当于先create再start</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker attach</strong> 可以进入一个后台运行的容器，比如 <strong>docker attach container_id</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker attach 15cb91256d66</span></div><div class="line">[root@15cb91256d66 /]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是attach命令不算好用，比如我们想要退出终端，就得exit了，这样容器也就退出了，还有一种方法<strong>docker exec -i -t container_id /bin/bash</strong> #可以临时打开一个虚拟终端，并且exit后，容器依然运行着</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -i -t 15cb91256d66 /bin/bash</span></div><div class="line">[root@15cb91256d66 /]<span class="comment">#</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -i -t centos-6-x86_64 /bin/bash</span></div><div class="line">[root@2983f5289991 /]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样进入了一个虚拟终端里面，我们可以运行一些命令，使用命令exit或者ctrl d 退出该bash，当退出后这个容器也会停止。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -d</strong> 可以让容器在后台运行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -d centos_with_net bash -c "while :; do echo 123; sleep 1; done "</span></div><div class="line">9afe4cdb5e63604a12045b104fa26008c84267665f27bf2bedebe5e6432b814f</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;停止镜像 <strong>docker stop container_id</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker stop 9afe</span></div><div class="line">9afe</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run –name web -itd centos bash</strong>  –name 给容器自定义名字</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -i -t -d --name web centos bash</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker logs 可以获取到容器的运行历史信息，用法如下<strong>docker logs container_id</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker logs 9afe</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker rm  container_id</strong> #container_id是ps的时候查看到的，这样就可以把所有container删除。如果是运行的容器，可以加-f</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker rm 9afe4cdb5e63</span></div><div class="line">9afe4cdb5e63</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果是运行的容器，可以加-f</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID IMAGE  COMMAND     CREATED       STATUS    PORTS NAMES</div><div class="line">6e35c7590c0e centos <span class="string">"/bin/bash"</span> 2 days ago Up 4 seconds       desperate_jepsen</div><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</div><div class="line">6e35c7590c0e centos <span class="string">"/bin/bash"</span> 2 days ago Up 7 seconds desperate_jepsen</div><div class="line">[root@docker ~]<span class="comment"># docker rm -f 6e35c7590c0e </span></div><div class="line">6e35c7590c0e</div><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run –rm -it centos bash -c “sleep 30”</strong> #–rm 可以让容器退出后直接删除，在这里命令执行完容器就会退出，不能和-d一起使用 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker export container_id &gt; file.tar 导出容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker ps</span></div><div class="line">CONTAINER ID IMAGE  COMMAND     CREATED         STATUS    PORTS    NAMES</div><div class="line">c42f598d215e centos <span class="string">"bash"</span>      16 hours ago Up 9 seconds            web</div><div class="line">914ff4a5efee centos <span class="string">"/bin/bash"</span> 27 hours ago Up 15 seconds high_mccarthy</div><div class="line">[root@docker ~]<span class="comment"># docker export c42f &gt; yanyi_centos.tar</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以迁移到其他机器上，需要导入<strong>cat file.tar |docker import - yanyi_centos</strong>#这样会生成yanyi_centos的镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># cat yanyi_centos.tar | docker import - yanyi_centos</span></div><div class="line">sha256:7092ed59698a2c1fbff09f883bb525d6d5fea78bbf96885ad5780459ca5bd06d</div><div class="line">[root@docker ~]<span class="comment"># docker images</span></div><div class="line">REPOSITORY   TAG    IMAGE ID    CREATED             SIZE</div><div class="line">yanyi_centos latest 7092ed59698a About a minute ago 191.8 MB</div></pre></td></tr></table></figure>
<h2 id="7-Docker-仓库管理"><a href="#7-Docker-仓库管理" class="headerlink" title="7.Docker 仓库管理"></a>7.Docker 仓库管理</h2><h3 id="下载-registry-镜像"><a href="#下载-registry-镜像" class="headerlink" title="下载 registry 镜像"></a>下载 registry 镜像</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker pull registry #下载registry 镜像，registy为docker官方提供的一个镜像，我们可以用它来创建本地的docker私有仓库。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker pull registry:0.6.7</span></div><div class="line">Trying to pull repository docker.io/library/registry ... </div><div class="line">0.6.7: Pulling from docker.io/library/registry</div><div class="line">a3ed95caeb02: Pull complete </div><div class="line">b744ac11e87c: Pull complete </div><div class="line">df38afb9684e: Pull complete </div><div class="line">0018b940d299: Pull complete </div><div class="line">0f24c060f901: Pull complete </div><div class="line">95ce9ef4df17: Pull complete </div><div class="line">89843edaf725: Pull complete </div><div class="line">d1350903a0ac: Pull complete </div><div class="line">a8f3ba806716: Pull complete </div><div class="line">edeb8014d2ad: Pull complete </div><div class="line">d0b78626245e: Pull complete </div><div class="line">Digest: sha256:b48ecc989fd0b79a16c2bfe59df5354e3fe85b562ef9f7361eac4bcd19585f5d</div><div class="line">Status: Downloaded newer image <span class="keyword">for</span> docker.io/registry:0.6.7</div></pre></td></tr></table></figure>
<h3 id="启动容器"><a href="#启动容器" class="headerlink" title="启动容器"></a>启动容器</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -d -p 5000:5000 registy</strong> #以registry镜像启动容器，监听5000端口</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -d -p 5000:5000 registry</span></div><div class="line">00361bd4d3390430d61db003b7375208e82902e4bb855e5cebfb210da743121f</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;curl 127.0.0.1:5000 #可以访问它</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># curl 127.0.0.1:5000 </span></div><div class="line"><span class="string">"docker-registry server (dev)"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># telnet 127.0.0.1 5000</span></div><div class="line">Trying 127.0.0.1...</div><div class="line">Connected to 127.0.0.1.</div><div class="line">Escape character is <span class="string">'^]'</span>.</div><div class="line">Connection closed by foreign host.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面我们来把其中一个<strong>镜像上传到私有仓库</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker tag docker.io/busybox 192.168.0.96:5000/busybox</strong> #标记一下tag，必须要带有私有仓库的ip:port </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker tag docker.io/busybox 192.168.0.96:5000/busybox</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker push 192.168.0.96:5000/busybox</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker push 192.168.0.96:5000/busybox</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时报错了类似如下</p>
<ul>
<li>cnetos 6</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Error response from daemon: invalid registry endpoint https://192.168.0.96:5000/v0/: unable to ping registry endpoint https://192.168.0.96:5000/v0/</div><div class="line">v2 ping attempt failed with error: Get https://192.168.0.96:5000/v2/: EOF</div><div class="line">v1 ping attempt failed with error: Get https://192.168.0.96:5000/v1/_ping: EOF. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.0.96:5000` to the daemon<span class="string">'s arguments. In the case of HTTPS, if you have access to the registry'</span>s CA certificate, no need <span class="keyword">for</span> the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.0.96:5000/ca.crt</div></pre></td></tr></table></figure>
<ul>
<li>centos 7</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">The push refers to a repository [192.168.0.96:5000/busybox]</div><div class="line">unable to ping registry endpoint https://192.168.0.96:5000/v0/</div><div class="line">v2 ping attempt failed with error: Get https://192.168.0.96:5000/v2/: EOF</div><div class="line">v1 ping attempt failed with error: Get https://192.168.0.96:5000/v1/_ping: EOF</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是因为Docker从1.3.X之后，与docker registry交互默认使用的是https，然而此处搭建的私有仓库只提供http服务，所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。解决该问题的方法为：</p>
<ul>
<li>centos 6</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># vim /etc/init.d/docker</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把 $exec -d $other_args 改为<br>$exec -d –insecure-registry 192.168.0.96:5000 $other_args</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后重启docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># service docker restart</span></div></pre></td></tr></table></figure>
<ul>
<li>centos 7</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># vim /etc/sysconfig/docker</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加上 ADD_REGISTRY=’–add-registry 192.168.0.96:5000’ 和 INSECURE_REGISTRY=’–insecure-registry 192.168.0.96:5000’</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后重启docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># systemctl restart docker</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再启动registry容器 <strong>docker start registry_container_id</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker start 00361bd4d339</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次把镜像上传私有仓库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker push 192.168.0.96:5000/busybox</span></div><div class="line">The push refers to a repository [192.168.0.96:5000/busybox]</div><div class="line">38ac8d0f5bb3: Pushed </div><div class="line">latest: digest: sha256:2efce9f5b0cb8815d192ae634b4c87943d0f0b873d98487ee98f8ed0504bd572 size: 527</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看私有仓库里面的所有镜像命令（版本不同命令不一样）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;curl <a href="http://192.168.0.96:5000/v1/search" target="_blank" rel="external">http://192.168.0.96:5000/v1/search</a>  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;curl <a href="http://192.168.0.96:5000/v2/_catalog" target="_blank" rel="external">http://192.168.0.96:5000/v2/_catalog</a> #最近版本（33.28m）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># curl http://192.168.0.96:5000/v2/_catalog</span></div><div class="line">&#123;<span class="string">"repositories"</span>:[<span class="string">"busybox"</span>]&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;curl <a href="http://192.168.0.96:5000/v2/image_name/tags/list" target="_blank" rel="external">http://192.168.0.96:5000/v2/image_name/tags/list</a> #最近版本（33.28m）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># curl http://192.168.0.96:5000/v2/busybox/tags/list </span></div><div class="line">&#123;<span class="string">"name"</span>:<span class="string">"busybox"</span>,<span class="string">"tags"</span>:[<span class="string">"latest"</span>]&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker search 192.168.0.96:5000 </p>
<h2 id="8-Docker-数据管理"><a href="#8-Docker-数据管理" class="headerlink" title="8.Docker 数据管理"></a>8.Docker 数据管理</h2><h3 id="挂载本地的目录到容器里"><a href="#挂载本地的目录到容器里" class="headerlink" title="挂载本地的目录到容器里"></a>挂载本地的目录到容器里</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -tid -v /data/:/data centos bash</strong> #-v 用来指定挂载目录，:前面的/data/为本地目录，:后面的/data/为容器里的目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># mkdir /data/</span></div><div class="line">[root@docker ~]<span class="comment"># docker run -itd -v /data/:/data1 centos bash</span></div><div class="line">cdb5c8a578d5f720df7a263a41d84c22118696f9e5dae7591f539fb4f83ecb16</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试，本地 data 目录下创建文件在容器内查看。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># touch /data/1.txt</span></div><div class="line">[root@docker ~]<span class="comment"># echo asdjfklasdjfklasdjf &gt; /data/1.txt</span></div><div class="line">[root@docker ~]<span class="comment"># cat /data/1.txt</span></div><div class="line">asdjfklasdjfklasdjf</div><div class="line">[root@docker ~]<span class="comment"># docker exec -it cdb5 bash</span></div><div class="line">[root@cdb5c8a578d5 /]<span class="comment"># ls /data1/</span></div><div class="line">1.txt</div><div class="line">[root@cdb5c8a578d5 /]<span class="comment"># cat /data1/1.txt</span></div><div class="line">asdjfklasdjfklasdjf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器创建文件，在本地目录查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@cdb5c8a578d5 /]<span class="comment"># touch /data1/2.txt</span></div><div class="line">[root@cdb5c8a578d5 /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># ls /data/</span></div><div class="line">1.txt 2.txt</div></pre></td></tr></table></figure>
<h3 id="挂载数据卷"><a href="#挂载数据卷" class="headerlink" title="挂载数据卷"></a>挂载数据卷</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其实我们挂载目录的时候，可以指定容器name，如果不指定就随机定义了。比如上面我们没有指定，它就生成了一个名字为angry_bell，这个名字可以使用命令 docker ps 看最右侧一列 docker run -itd –volumes-from angry_bell centos bash</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd --volumes-from angry_bell centos bash</span></div><div class="line">e36d478d9253a4758b28fb3a3bc5596781b4e572797aee2623bfcf5f50d6492d</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样，我们使用 centos 镜像创建了新的容器，并且使用了 angry_bell 容器的数据卷。我们可以在新容器的数据卷下创建文件，然后去 angry_bell 容器里查看。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it e36 bash</span></div><div class="line">[root@e36d478d9253 /]<span class="comment"># ls /data1</span></div><div class="line">1.txt 2.txt</div><div class="line">[root@e36d478d9253 /]<span class="comment"># touch /data1/3.txt</span></div><div class="line">[root@e36d478d9253 /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># docker exec -it cdb bash</span></div><div class="line">[root@cdb5c8a578d5 /]<span class="comment"># ls /data1/</span></div><div class="line">1.txt 2.txt 3.txt</div></pre></td></tr></table></figure>
<h3 id="定义数据卷容器"><a href="#定义数据卷容器" class="headerlink" title="定义数据卷容器"></a>定义数据卷容器</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候，我们需要多个容器之间相互共享数据，类似于linux里面的NFS，所以就可以搭建一个专门的数据卷容器，然后其他容器直接挂载该数据卷。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先建立数据卷容器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -itd -v /data/ –name centos_testvol centos bash</strong>  #注意这里的/data/是容器的/data目录，并非本地的/data/目录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd -v /data/ --name centos_testvol centos bash</span></div><div class="line">cfe772a30a89886d9603e84f3078052a39f6119d8b9561752d5d2eb83b245835</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后让其他容器挂载该数据卷 <strong>docker run -itd –volumes-from centos_testvol centos bash</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd --volumes-from centos_testvol centos bash</span></div><div class="line">d3cd4e6611e18226ceb4f4622bd55c534fb313865566cc7819556b3a1a53e530</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看是否成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it d3c bash</span></div><div class="line">[root@d3cd4e6611e1 /]<span class="comment"># ls /data</span></div><div class="line">[root@d3cd4e6611e1 /]<span class="comment"># mkdir /data/test</span></div><div class="line">[root@d3cd4e6611e1 /]<span class="comment"># touch /data/test/1.txt</span></div><div class="line">[root@d3cd4e6611e1 /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># docker exec -it centos_testvol bash </span></div><div class="line">[root@cfe772a30a89 /]<span class="comment"># ls /data</span></div><div class="line"><span class="built_in">test</span></div><div class="line">[root@cfe772a30a89 /]<span class="comment"># ls /data/test</span></div><div class="line">1.txt</div></pre></td></tr></table></figure>
<h3 id="Docker-数据管理-数据卷的备份与恢复"><a href="#Docker-数据管理-数据卷的备份与恢复" class="headerlink" title="Docker 数据管理 - 数据卷的备份与恢复"></a>Docker 数据管理 - 数据卷的备份与恢复</h3><h4 id="备份"><a href="#备份" class="headerlink" title="备份"></a>备份</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># mkdir /vol_data_backup</span></div><div class="line">[root@docker ~]<span class="comment"># docker run -itd --volumes-from centos_testvol -v /vol_data_backup/:/backup centos bash </span></div><div class="line">b246db616639574e1d6fe5ceb3534337df71676af3362d1b4dfd3b627523be30</div><div class="line">[root@docker ~]<span class="comment"># docker exec -it b246 bash</span></div><div class="line">[root@b246db616639 /]<span class="comment"># tar cvf /backup/data.tar /data/</span></div><div class="line">tar: Removing leading `/<span class="string">' from member names</span></div><div class="line"><span class="string">/data/</span></div><div class="line"><span class="string">/data/test/</span></div><div class="line"><span class="string">/data/test/1.txt</span></div><div class="line"><span class="string">[root@b246db616639 /]# ls /backup</span></div><div class="line"><span class="string">data.tar</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以直接执行命令：<strong>docker run –volumes-from centos_testvol -v  /vol_data_backup/:/backup centos tar cvf  /backup/data.tar /data/</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run --volumes-from centos_testvol -v /vol_data_backup/:/backup centos tar cvf /backup/data.tar /data/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：首先我们需要使用testvol数据卷新开一个容器，同时我们还需要把本地的/vol_data_backup/目录挂载到该容器的/backup下，这样在容器中/backup目录里面新建的文件，我们就可以直接在/vol_data_backup/目录中看到了。 然后再把/data/目录下面的文件打包到成data.tar文件放到/backup目录下面。</p>
<h4 id="恢复"><a href="#恢复" class="headerlink" title="恢复"></a>恢复</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;思路： 先新建一个数据卷容器，再建一个新的容器并挂载该数据卷容器，然后再把tar包解包。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建数据卷容器：<strong>docker run -itd -v /data/ –name centos_testvol2 centos bash</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd -v /data/ --name centos_testvol2 centos bash</span></div><div class="line">00e733d06ece150b114490a36f0a751bed4f43de9aaede13e74331fcdeb87518</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;挂载数据卷新建容器，并解包：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd --volumes-from centos_testvol2 -v /vol_data_backup/:/backup centos bash</span></div><div class="line">3f9a5887e0dac36b9bd29c5e0925a27d627b39f1945bed66d471e8bccc0cde70</div><div class="line">[root@docker ~]<span class="comment"># docker exec -it 3f9a bash</span></div><div class="line">[root@3f9a5887e0da /]<span class="comment"># tar xvf /backup/data.tar</span></div><div class="line">data/</div><div class="line">data/<span class="built_in">test</span>/</div><div class="line">data/<span class="built_in">test</span>/1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以执行命令：<strong>docker run –volumes-from centos_testvol2 -v /vol_data_backup/:/backup centos tar xvf /backup/data.tar</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run --volumes-from centos_testvol2 -v /vol_data_backup/:/backup centos tar xvf /backup/data.tar</span></div></pre></td></tr></table></figure>
<h2 id="9-Docker-网络管理-四种网络模式"><a href="#9-Docker-网络管理-四种网络模式" class="headerlink" title="9.Docker 网络管理 - 四种网络模式"></a>9.Docker 网络管理 - 四种网络模式</h2><h3 id="host模式"><a href="#host模式" class="headerlink" title="host模式"></a>host模式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;host模式，使用docker run时使用–net=host指定</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker使用的网络实际上和宿主机一样，在容器内看到的网卡ip是宿主机上的ip</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -it --rm --net=host centos_with_net bash</span></div><div class="line">[root@docker /]<span class="comment"># ifconfig</span></div><div class="line">docker0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">         inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0</div><div class="line">         inet6 fe80::42:69ff:fe81:dca8 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">         ether 02:42:69:81:dc:a8 txqueuelen 0 (Ethernet)</div><div class="line">         RX packets 1551 bytes 79846 (77.9 KiB)</div><div class="line">         RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">         TX packets 1642 bytes 8892494 (8.4 MiB)</div><div class="line">         TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line"></div><div class="line">ens33: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">       inet 192.168.0.96 netmask 255.255.255.0 broadcast 192.168.0.255</div><div class="line">       inet6 fe80::b5f:e696:2650:f930 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">       ether 00:0c:29:5a:19:a5 txqueuelen 1000 (Ethernet)</div><div class="line">       RX packets 14059 bytes 9848234 (9.3 MiB)</div><div class="line">       RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">       TX packets 5604 bytes 576835 (563.3 KiB)</div><div class="line">       TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line">[root@docker /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># ifconfig</span></div><div class="line">docker0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">         inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0</div><div class="line">         inet6 fe80::42:69ff:fe81:dca8 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">         ether 02:42:69:81:dc:a8 txqueuelen 0 (Ethernet)</div><div class="line">         RX packets 1551 bytes 79846 (77.9 KiB)</div><div class="line">         RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">         TX packets 1642 bytes 8892494 (8.4 MiB)</div><div class="line">         TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line"></div><div class="line">ens33: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">       inet 192.168.0.96 netmask 255.255.255.0 broadcast 192.168.0.255</div><div class="line">       inet6 fe80::b5f:e696:2650:f930 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">       ether 00:0c:29:5a:19:a5 txqueuelen 1000 (Ethernet)</div><div class="line">       RX packets 14084 bytes 9850194 (9.3 MiB)</div><div class="line">       RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">       TX packets 5620 bytes 584957 (571.2 KiB)</div><div class="line">       TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div></pre></td></tr></table></figure>
<h3 id="container模式"><a href="#container模式" class="headerlink" title="container模式"></a>container模式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;container模式，使用–net=container:container_id/container_name</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;多个容器使用共同的网络，看到的ip是一样的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it c07 /bin/bash</span></div><div class="line">[root@c076b8953e9d /]<span class="comment"># ifconfig</span></div><div class="line">eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">      inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0</div><div class="line">      inet6 fe80::42:acff:fe11:3 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">      ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)</div><div class="line">      RX packets 66 bytes 5364 (5.2 KiB)</div><div class="line">      RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">      TX packets 8 bytes 648 (648.0 B)</div><div class="line">      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line"></div><div class="line">lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt; mtu 65536</div><div class="line">    inet 127.0.0.1 netmask 255.0.0.0</div><div class="line">    inet6 ::1 prefixlen 128 scopeid 0x10&lt;host&gt;</div><div class="line">    loop txqueuelen 1 (Local Loopback)</div><div class="line">    RX packets 0 bytes 0 (0.0 B)</div><div class="line">    RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">    TX packets 0 bytes 0 (0.0 B)</div><div class="line">    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line"></div><div class="line">[root@c076b8953e9d /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># docker run -it --rm --net=container:c076 centos_with_net bash</span></div><div class="line">[root@c076b8953e9d /]<span class="comment"># ifconfig</span></div><div class="line">eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">      inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0</div><div class="line">      inet6 fe80::42:acff:fe11:3 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">      ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)</div><div class="line">      RX packets 66 bytes 5364 (5.2 KiB)</div><div class="line">      RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">      TX packets 8 bytes 648 (648.0 B)</div><div class="line">      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div><div class="line"></div><div class="line">lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt; mtu 65536</div><div class="line">    inet 127.0.0.1 netmask 255.0.0.0</div><div class="line">    inet6 ::1 prefixlen 128 scopeid 0x10&lt;host&gt;</div><div class="line">    loop txqueuelen 1 (Local Loopback)</div><div class="line">    RX packets 0 bytes 0 (0.0 B)</div><div class="line">    RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">    TX packets 0 bytes 0 (0.0 B)</div><div class="line">    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div></pre></td></tr></table></figure>
<h3 id="none模式"><a href="#none模式" class="headerlink" title="none模式"></a>none模式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;none模式，使用–net=none指定</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这种模式下，不会配置任何网络</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -it --rm --net=none centos_with_net bash</span></div><div class="line">[root@069e635bea5f /]<span class="comment"># ifconfig</span></div><div class="line">lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt; mtu 65536</div><div class="line">    inet 127.0.0.1 netmask 255.0.0.0</div><div class="line">    inet6 ::1 prefixlen 128 scopeid 0x10&lt;host&gt;</div><div class="line">    loop txqueuelen 1 (Local Loopback)</div><div class="line">    RX packets 0 bytes 0 (0.0 B)</div><div class="line">    RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">    TX packets 0 bytes 0 (0.0 B)</div><div class="line">    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div></pre></td></tr></table></figure>
<h3 id="bridge模式"><a href="#bridge模式" class="headerlink" title="bridge模式"></a>bridge模式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bridge模式，使用–net=bridge指定</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认模式，不用指定默认就是这种网络模式。这种模式会为每个容器分配一个独立的   Network Namespace。类似于vmware的nat网络模式。同一个宿主机上的所有容器会在同一 个网段下，相互之间是可以通信的。</p>
<h2 id="10-Docker-网络管理-外部访问容器"><a href="#10-Docker-网络管理-外部访问容器" class="headerlink" title="10.Docker 网络管理 - 外部访问容器"></a>10.Docker 网络管理 - 外部访问容器</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先使用centos镜像新建一个容器，然后在该容器中安装httpd服务，并启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it c076 /bin/bash</span></div><div class="line">[root@c076b8953e9d /]<span class="comment"># yum install httpd</span></div><div class="line">[root@c076b8953e9d /]<span class="comment"># /usr/sbin/httpd</span></div><div class="line">AH00558: httpd: Could not reliably determine the server<span class="string">'s fully qualified domain name, using 172.17.0.3. Set the '</span>ServerName<span class="string">' directive globally to suppress this message</span></div><div class="line"><span class="string">[root@c076b8953e9d /]# ps aux |grep httpd</span></div><div class="line"><span class="string">root    92 0.1 0.1  221920  3452  ?  Ss 12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">apache  93 0.0 0.1  221920  2944  ?  S  12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">apache  94 0.0 0.1  221920  2944  ?  S  12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">apache  95 0.0 0.1  221920  2944  ?  S  12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">apache  96 0.0 0.1  221920  2944  ?  S  12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">apache  97 0.0 0.1  221920  2944  ?  S  12:57 0:00 /usr/sbin/httpd</span></div><div class="line"><span class="string">root   101 0.0 0.0    9032   656  ?  S+ 12:57 0:00 grep --color=auto httpd</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再把该容器导成一个新的镜像（centos_httpd)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker commit -m "centos_httpd" -a "yanyi" c076 centos_httpd</span></div><div class="line">sha256:12fb12486cfe16f66686b55e6c6365c5ecac3211fd97720f9e6e3350da757ed6</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后再使用新镜像创建容器，并指定端口映射 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker run -itd -p 5123:80 centos_httpd bash</strong>  #-p 可以指定端口映射，本例中将容器的80端口映射为本地的5123端口</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd -p 5123:80 centos_httpd bash</span></div><div class="line">bbb931e95f37c4202634afa285fbc70c762c3a2a292b0a7655a08ed807ebee43</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>docker exec -it container_id  bash</strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it bbb9 bash</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动httpd： httpd -k start </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@bbb931e95f37 /]<span class="comment"># httpd -k start</span></div><div class="line">AH00558: httpd: Could not reliably determine the server<span class="string">'s fully qualified domain name, using 172.17.0.11. Set the '</span>ServerName<span class="string">' directive globally to suppress this message</span></div><div class="line"><span class="string">[root@bbb931e95f37 /]# ps aux |grep httpd</span></div><div class="line"><span class="string">root   29 0.0 0.1 221920 3456 ? Ss 13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">apache 30 0.0 0.1 221920 2940 ? S  13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">apache 31 0.0 0.1 221920 2940 ? S  13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">apache 32 0.0 0.1 221920 2940 ? S  13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">apache 33 0.0 0.1 221920 2940 ? S  13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">apache 34 0.0 0.1 221920 2940 ? S  13:09 0:00 httpd -k start</span></div><div class="line"><span class="string">root   36 0.0 0.0   9032  656 ? S+ 13:10 0:00 grep --color=auto httpd</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑1.html： vim /var/www/html/1.html 随便写点东西</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@bbb931e95f37 /]<span class="comment"># vim /var/www/html/1.html</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@bbb931e95f37 /]<span class="comment"># curl localhost/1.html</span></div><div class="line">yanyi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;退出该容器：exit</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试： curl 127.0.0.1:5123/1.html </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@bbb931e95f37 /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># curl 127.0.0.1:5123/1.html</span></div><div class="line">yanyi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-p后面也支持IP:port:ip:port 的格式，比如</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-p 127.0.0.1:8080:80 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以不写本地的端口，只写ip，这样会随意分配一个端口</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-p 127.0.0.1::80 </p>
<h2 id="11-Docker-网络管理-–-容器互联"><a href="#11-Docker-网络管理-–-容器互联" class="headerlink" title="11.Docker 网络管理 – 容器互联"></a>11.Docker 网络管理 – 容器互联</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载一个mysql镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker pull mysql</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建一个容器，命名为db</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -it -d -p 13306:3306 --name db mysql bash</span></div><div class="line">53247dfe72258636e13b133ee082645cacd48ab6abc4e14442566250c6cd3c24</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在新建一个web容器，并和db互联</p>
<p>docker run -it -d -p 12308:80 –name web –link db:db centos-httpd bash</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在web上运行env命令可以查看到关于db的环境变量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因 pull 的 mydql 是  Ubuntu 版本，则重新制作 centos 版本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos7 yum 没有 mysql-server ，则创建 centos6 容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd centos-6-x86 bash</span></div><div class="line">3a40c6cd247c6c7083caf2579935fb497fef6b8ca021097766b2d2585ce0642a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;yum 安装 mysql-server</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it 3a4 bash</span></div><div class="line">[root@3a40c6cd247c /]<span class="comment"># yum install -y mysql.server</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@3a40c6cd247c /]<span class="comment"># exit</span></div><div class="line">[root@docker ~]<span class="comment"># docker commit -m "centos-6-mysql" -a "yanyi" 3a40 centos6-mysql</span></div><div class="line">sha256:7cf0e61cef00084ad7792b8a336490c8d3a53819cc1fa1c1fc8071c8f2f8f8de</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建一个容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd -p 13306:3306 centos6-mysql bash</span></div><div class="line">777952322ff56b7c3ab4126e41a4a83a105c04fd785b7d52916d5b8e4a57c17a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再新建一个 web 容器，并和 mysql 容器互联。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd -p 18080:80 --name web --link elegant_liskov:db centos_httpd bash </span></div><div class="line">d6ec87f1e233256d11fff15d70ce602c1e9163686f4ef128e90bce3ac31fa816</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在web上运行env命令可以查看到关于db的环境变量</p>
<h2 id="12-Docker-网络管理"><a href="#12-Docker-网络管理" class="headerlink" title="12.Docker 网络管理"></a>12.Docker 网络管理</h2><h3 id="配置桥接网络-centos7"><a href="#配置桥接网络-centos7" class="headerlink" title="配置桥接网络(centos7)"></a>配置桥接网络(centos7)</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了使本地网络中的机器和Docker容器更方便的通信，我们经常会有将Docker容器配置到和主机同一网段的需求。这个需求其实很容易实现，我们只要将Docker容器和宿主机的网卡桥接起来，再给Docker容器配上IP就可以了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装 brctl 命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># yum install bridge-utils</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑网卡配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># cd /etc/sysconfig/network-scripts/</span></div><div class="line">[root@docker network-scripts]<span class="comment"># cp ifcfg-ens33 ifcfg-br0</span></div><div class="line">[root@docker network-scripts]<span class="comment"># vim ifcfg-ens33</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置 ifcfg-ens33 #增加BRIDGE=br0，删除IPADDR,NETMASK,GATEWAY,DNS1</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置 ifcfg-br0 #修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># vim ifcfg-br0</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启相关服务，以及关闭相关服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># systemctl stop NetworkManager</span></div><div class="line">[root@docker network-scripts]<span class="comment"># systemctl disable NetworkManager</span></div><div class="line">Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.</div><div class="line">Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.</div><div class="line">Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># systemctl restart network</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;br0 正常启动，能够正常访问该机器即可，再用下面命令查看有即可： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># brctl show </span></div><div class="line">bridge name    bridge id             STP enabled     interfaces</div><div class="line">br0            8000.000c295a19a5     yes             ens33</div><div class="line">docker0        8000.0242c3f92b21     no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改docker配置文件，修改桥接的网卡以及IP分配范围</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># vim /etc/sysconfig/docker</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将：OPTIONS=’–selinux-enabled’</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为：OPTIONS=’–selinux-enabled -b=br0 –fixed-cidr=192.168.0.198/26’</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重新启动docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># systemctl stop docker.service</span></div><div class="line">[root@docker network-scripts]<span class="comment"># systemctl start docker.service</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建 docker 容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd centos7 bash</span></div><div class="line">6cb69dbd31dd3845fc0467c436942d3cf3c08b035b24d26f43433e7c761e0043</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入容器，查看 IP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it 6cb6 bash</span></div><div class="line">[root@6cb69dbd31dd /]<span class="comment"># ifconfig</span></div><div class="line">eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu 1500</div><div class="line">      inet 192.168.0.192 netmask 255.255.255.0 broadcast 0.0.0.0</div><div class="line">      inet6 fe80::42:c0ff:fea8:c0 prefixlen 64 scopeid 0x20&lt;link&gt;</div><div class="line">      ether 02:42:c0:a8:00:c0 txqueuelen 0 (Ethernet)</div><div class="line">      RX packets 1880 bytes 8921311 (8.5 MiB)</div><div class="line">      RX errors 0 dropped 0 overruns 0 frame 0</div><div class="line">      TX packets 1671 bytes 116955 (114.2 KiB)</div><div class="line">      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</div></pre></td></tr></table></figure>
<h3 id="Docker-网络管理-–-配置桥接网络-centos6"><a href="#Docker-网络管理-–-配置桥接网络-centos6" class="headerlink" title="Docker 网络管理 – 配置桥接网络(centos6)"></a>Docker 网络管理 – 配置桥接网络(centos6)</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># cd /etc/sysconfig/network-scripts/</span></div><div class="line">[root@docker network-scripts]<span class="comment"># cp ifcfg-eth0 ifcfg-br0</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置 ifcfg-eth0 #增加BRIDGE=br0，删除IPADDR,NETMASK,GATEWAY,DNS1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># vim ifcfg-eth0</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置 ifcfg-br0 #修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># vim ifcfg-br0</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker network-scripts]<span class="comment"># service network restat</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装 git</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># yum install -y git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装pipwork:  git clone <a href="https://github.com/jpetazzo/pipework" target="_blank" rel="external">https://github.com/jpetazzo/pipework</a>;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># git clone https://github.com/jpetazzo/pipework</span></div><div class="line">Initialized empty Git repository <span class="keyword">in</span> /root/pipework/.git/</div><div class="line">remote: Counting objects: 489, <span class="keyword">done</span>.</div><div class="line">remote: Total 489 (delta 0), reused 0 (delta 0), pack-reused 489</div><div class="line">Receiving objects: 100% (489/489), 167.74 KiB | 28 KiB/s, <span class="keyword">done</span>.</div><div class="line">Resolving deltas: 100% (258/258), <span class="keyword">done</span>.</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># cp ~/pipework/pipework /usr/local/bin/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开启一个容器: docker run -itd –net=none –name yanyi centos6-httpd bash</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker run -itd --net=none --name yanyi centos6-httpd bash</span></div><div class="line"> e558b8afb9fc781334fdec93b7b23103bde8bda2a46d2ed14f5b6668d9b99e05</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rpm -Uvh <a href="https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm" target="_blank" rel="external">https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm</a> #不安会报错Object “netns” is unknown, try “ip help”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># rpm -Uvh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm</span></div><div class="line">Retrieving https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm</div><div class="line">warning: /var/tmp/rpm-tmp.fja3UU: Header V4 RSA/SHA1 Signature, key ID d97b3247: NOKEY</div><div class="line">Preparing... <span class="comment">########################################### [100%]</span></div><div class="line"> 1:iproute <span class="comment">########################################### [100%]</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;给容器配置一个 IP ：pipework br0 yanyi 192.168.0.199/24@192.168.0.1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># pipework br0 yanyi 192.168.0.199/24@192.168.0.1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker exec -it yanyi bash</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进去后ifconfig查看就可以看到新添加的ip</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<h2 id="13-dockerfile-格式"><a href="#13-dockerfile-格式" class="headerlink" title="13. dockerfile 格式"></a>13. dockerfile 格式</h2><h3 id="Dockerfile创建镜像-–-Dockerfile格式"><a href="#Dockerfile创建镜像-–-Dockerfile格式" class="headerlink" title="Dockerfile创建镜像 – Dockerfile格式"></a>Dockerfile创建镜像 – Dockerfile格式</h3><ul>
<li>FROM   #指定基于哪个基础镜像</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式 FROM <image> 或者  FROM <image>:<tag>，  比如</tag></image></image></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">FROM centos</div><div class="line">FROM centos:latest</div></pre></td></tr></table></figure>
<ul>
<li>MAINTAINER  #指定作者信息</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式  MAINTAIN <name> ，比如</name></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">MAINTAINER  yanyi  hcldir@qq.com</div></pre></td></tr></table></figure>
<ul>
<li>RUN   #镜像操作指令</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式为 RUN <command>  或者 RUN [“executable”, “param1”, “param2”]，比如</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">RUN  yum install  httpd</div><div class="line">RUN [<span class="string">"/bin/bash"</span>, <span class="string">"-c"</span>, <span class="string">"echo hello"</span>]</div></pre></td></tr></table></figure>
<ul>
<li>CMD  # 三种格式：<ul>
<li>CMD [“executable”, “param1”, “param2”]</li>
<li>CMD command param1 param2</li>
<li>CMD [“param1”, “param2”]</li>
</ul>
</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RUN和CMD看起来挺像，但是CMD用来指定容器启动时用到的命令，只能有一条。比如</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CMD [<span class="string">"/bin/bash"</span>, <span class="string">"/usr/local/nginx/sbin/nginx"</span>, <span class="string">"-c"</span>,  <span class="string">"/usr/local/nginx/conf/nginx.conf"</span>]</div></pre></td></tr></table></figure>
<ul>
<li>EXPOSE  </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式为 EXPOSE <port> [<port>…] , 比如</port></port></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">EXPOSE 22 80 8443</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个用来指定要映射出去的端口，比如容器内部我们启动了sshd和nginx，所以我们需要把22和80端口暴漏出去。这个需要配合-P（大写）来工作，也就是说在启动容器时，需要加上-P，让它自动分配。如果想指定具体的端口，也可以使用-p（小写）来指定。</p>
<ul>
<li>ENV  </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式 ENV  <key> <value>, 比如  </value></key></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ENV PATH /usr/<span class="built_in">local</span>/mysql/bin:<span class="variable">$PATH</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它主要是为后续的RUN指令提供一个环境变量，我们也可以定义一些自定义的变量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ENV MYSQL_version 5.6</p>
<ul>
<li>ADD  格式 add <src> <dest></dest></src></li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将本地的一个文件或目录拷贝到容器的某个目录里。 其中src为Dockerfile所在目录的相对路径，它也可以是一个url。比如</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ADD &lt;conf/vhosts&gt; &lt;/usr/<span class="built_in">local</span>/nginx/conf&gt;</div></pre></td></tr></table></figure>
<ul>
<li>COPY  </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式同add</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用方法和add一样，不同的是，它不支持url</p>
<ul>
<li>ENTRYPOINT  格式类似CMD</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器启动时要执行的命令，它和CMD很像，也是只有一条生效，如果写多个只有最后一条有效。和CMD不同是：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CMD 是可以被 docker run 指令覆盖的，而ENTRYPOINT不能覆盖。比如，容器名字为yanyi</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们在Dockerfile中指定如下CMD：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CMD [“/bin/<span class="built_in">echo</span>”, “<span class="built_in">test</span> ”]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动容器的命令是  docker run yanyi 这样会输出 test</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如启动容器的命令是 docker run -it yanyi  /bin/bash  什么都不会输出</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ENTRYPOINT不会被覆盖，而且会比CMD或者docker run指定的命令要靠前执行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ENTRYPOINT [“echo”, “test”]</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker run -it yanyi  123</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;则会输入 test  123 ，这相当于要执行命令  echo test  123 </p>
<ul>
<li>VOLUME</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式  VOLUME [“/data”]</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个可以从本地主机或其他容器挂载的挂载点。</p>
<ul>
<li>USER  </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式 USER daemon</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定运行容器的用户</p>
<ul>
<li>WORKDIR  </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式 WORKDIR  /path/to/workdir</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为后续的RUN、CMD或者ENTRYPOINT指定工作目录</p>
<h2 id="14-dockerfile-示例-安装-nginx"><a href="#14-dockerfile-示例-安装-nginx" class="headerlink" title="14. dockerfile 示例-安装 nginx"></a>14. dockerfile 示例-安装 nginx</h2><h3 id="Dockerfile创建镜像-–-Dockerfile示例"><a href="#Dockerfile创建镜像-–-Dockerfile示例" class="headerlink" title="Dockerfile创建镜像 – Dockerfile示例"></a>Dockerfile创建镜像 – Dockerfile示例</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先下载nginx的配置文件 wget <a href="http://www.apelearn.com/study_v2/.nginx_conf" target="_blank" rel="external">http://www.apelearn.com/study_v2/.nginx_conf</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># wget http://www.apelearn.com/study_v2/.nginx_conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建 Dockerfile</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># vim Dockerfile</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="comment">############################################################</span></div><div class="line"><span class="comment"># Dockerfile to build Nginx Installed Containers</span></div><div class="line"><span class="comment"># Based on CentOS</span></div><div class="line"><span class="comment">############################################################</span></div><div class="line"><span class="comment"># Set the base image to CentOS</span></div><div class="line">FROM centos6</div><div class="line"><span class="comment"># File Author / Maintainer</span></div><div class="line">MAINTAINER yanyi hcldir@qq.com</div><div class="line"></div><div class="line"><span class="comment"># Install necessary tools</span></div><div class="line">RUN yum install -y pcre-devel wget net-tools gcc</div><div class="line">RUN yum install -y zlib zlib-devel make</div><div class="line">RUN yum install -y openssl-devel</div><div class="line"><span class="comment"># Install Nginx</span></div><div class="line">ADD http://nginx.org/download/nginx-1.8.0.tar.gz .</div><div class="line">RUN tar zxvf nginx-1.8.0.tar.gz</div><div class="line">RUN mkdir -p /usr/<span class="built_in">local</span>/nginx</div><div class="line">RUN <span class="built_in">cd</span> nginx-1.8.0 &amp;&amp; ./configure --prefix=/usr/<span class="built_in">local</span>/nginx &amp;&amp; make &amp;&amp; make install</div><div class="line">RUN rm -fv /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</div><div class="line">COPY .nginx_conf /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</div><div class="line"></div><div class="line"><span class="comment"># Expose ports</span></div><div class="line">EXPOSE 80</div><div class="line"><span class="comment"># Set the default command to execute</span></div><div class="line"><span class="comment"># when creating a new container</span></div><div class="line">ENTRYPOINT /usr/<span class="built_in">local</span>/nginx/sbin/nginx -c /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Docker/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建镜像：docker build -t centos_nginx  .</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker build -t centos-nginx .</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docker  images 可以看到我们新建的镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@docker ~]<span class="comment"># docker images</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Dns、Iredmaill/8. DNS服务器(二)：使用bind实现主从DNS服务器数据同步/">DNS服务器(二)：使用bind实现主从DNS服务器数据同步</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/DNS/">DNS</a>
			</span>
		
	</div>

	

	
		<h2 id="一、bind简介"><a href="#一、bind简介" class="headerlink" title="一、bind简介"></a>一、bind简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux中通常使用bind来实现DNS服务器的架设，bind软件由isc(www.isc.org)维护。在yum仓库中可以找到软件，配置好yum源，直接使用命令yum install bind就可以安装。当前bind的稳定版本为bind9，bind的服务名称为named，监听的端口为53号端口。bind的主要配置文件为/etc/named.conf，此文件主要用于配置区域，并指定区域数据库文件名称。区域数据库文件通常保存于/var/named/目录下，用于定义区域的资源类型。</p>
<h2 id="二、使用bind架设DNS服务器"><a href="#二、使用bind架设DNS服务器" class="headerlink" title="二、使用bind架设DNS服务器"></a>二、使用bind架设DNS服务器</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例操作：以域名wubinary.com为例配置一个DNS服务器，实现正向解析与反向解析。</p>
<h3 id="1-使用setup命令配置DNS服务器的IP地址，我们以192-168-0-70这个IP地址为例，在本地架设一个DNS服务器。"><a href="#1-使用setup命令配置DNS服务器的IP地址，我们以192-168-0-70这个IP地址为例，在本地架设一个DNS服务器。" class="headerlink" title="1.使用setup命令配置DNS服务器的IP地址，我们以192.168.0.70这个IP地址为例，在本地架设一个DNS服务器。"></a>1.使用setup命令配置DNS服务器的IP地址，我们以192.168.0.70这个IP地址为例，在本地架设一个DNS服务器。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/01.jpeg?raw=true)</p>
<h3 id="2-bind配置文件为-etc-named-conf，此文件用于定义区域。每个区域的数据文件保存在-var-named目录下。"><a href="#2-bind配置文件为-etc-named-conf，此文件用于定义区域。每个区域的数据文件保存在-var-named目录下。" class="headerlink" title="2.bind配置文件为/etc/named.conf，此文件用于定义区域。每个区域的数据文件保存在/var/named目录下。"></a>2.bind配置文件为/etc/named.conf，此文件用于定义区域。每个区域的数据文件保存在/var/named目录下。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;named.conf各参数项说明：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">options &#123;</div><div class="line">//全局选项</div><div class="line">&#125;</div><div class="line">zone <span class="string">"ZONE name"</span>&#123;</div><div class="line">//定义区域</div><div class="line">&#125;</div><div class="line">logging&#123;</div><div class="line">//定义日志系统</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;named.conf文件内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"> options &#123;</div><div class="line"> listen-on port 53 &#123; 127.0.0.1; &#125;; <span class="comment">#定义监听端口及IP地址</span></div><div class="line"> listen-on-v6 port 53 &#123; ::1; &#125;; <span class="comment">#定义监听的IPv6地址</span></div><div class="line"> directory  <span class="string">"/var/named"</span>; <span class="comment">#全局目录</span></div><div class="line"> dump-file  <span class="string">"/var/named/data/cache_dump.db"</span>;</div><div class="line"> statistics-file <span class="string">"/var/named/data/named_stats.txt"</span>;</div><div class="line"> memstatistics-file <span class="string">"/var/named/data/named_mem_stats.txt"</span>;</div><div class="line"> allow-query &#123; localhost; &#125;;<span class="comment">#允许查询的IP地址</span></div><div class="line"> recursion yes; <span class="comment">#是否允许递归查询</span></div><div class="line"> dnssec-enable yes;</div><div class="line"> dnssec-validation yes;</div><div class="line"> dnssec-lookaside auto;</div><div class="line"> /* Path to ISC DLV key */</div><div class="line"> bindkeys-file <span class="string">"/etc/named.iscdlv.key"</span>;</div><div class="line"> managed-keys-directory <span class="string">"/var/named/dynamic"</span>;</div><div class="line">&#125;;</div><div class="line">logging &#123;</div><div class="line"> channel default_debug &#123;</div><div class="line"> file <span class="string">"data/named.run"</span>;</div><div class="line"> severity dynamic;</div><div class="line"> &#125;;</div><div class="line">&#125;;</div><div class="line">zone <span class="string">"."</span> IN &#123;</div><div class="line"> <span class="built_in">type</span> hint;</div><div class="line"> file <span class="string">"named.ca"</span>;</div><div class="line">&#125;;</div><div class="line">include <span class="string">"/etc/named.rfc1912.zones"</span>;</div><div class="line">include <span class="string">"/etc/named.root.key"</span>;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：bind的配置文件/etc/named.conf里必须要定义的三个区域是：根、127.0.0.1和127.0.0.1的反解。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上options选项中有许多是我们用不到，我们先把它们注释掉。结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">options &#123;</div><div class="line">// listen-on port 53 &#123; 127.0.0.1; &#125;;</div><div class="line">// listen-on-v6 port 53 &#123; ::1; &#125;;</div><div class="line"> directory  <span class="string">"/var/named"</span>;</div><div class="line"> dump-file  <span class="string">"/var/named/data/cache_dump.db"</span>;</div><div class="line"> statistics-file <span class="string">"/var/named/data/named_stats.txt"</span>;</div><div class="line"> memstatistics-file <span class="string">"/var/named/data/named_mem_stats.txt"</span>;</div><div class="line">// allow-query &#123; localhost; &#125;;</div><div class="line">// recursion yes;</div><div class="line">// dnssec-enable yes;</div><div class="line">// dnssec-validation yes;</div><div class="line">// dnssec-lookaside auto;</div><div class="line"> /* Path to ISC DLV key */</div><div class="line">// bindkeys-file <span class="string">"/etc/named.iscdlv.key"</span>;</div><div class="line">// managed-keys-directory <span class="string">"/var/named/dynamic"</span>;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<h3 id="3-打开-etc-named-rfc1912-zones文件，添加一个区域。"><a href="#3-打开-etc-named-rfc1912-zones文件，添加一个区域。" class="headerlink" title="3.打开/etc/named.rfc1912.zones文件，添加一个区域。"></a>3.打开/etc/named.rfc1912.zones文件，添加一个区域。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/02.jpeg?raw=true)</p>
<ul>
<li>type: 用于定义区域类型，此时只有一个DNS服务器，所以为master，type可选值为：hint(根的)|master(主的)|slave(辅助的)|forward(转发)</li>
<li>file：用于定义区域数据文件路径，默认该文件保存在/var/named/目录。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;区域添加好后，使用命令：named-checkconf 或 service named configtest测试配置文件语法格式。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/03.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出错了！原来少了一个分号，配置文件的格式是每行后面都必须加分号结束，并且有花括号的地方，花括号两边必须要有空格。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/04.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;没有提示则表示文件语法正常。</p>
<h3 id="4-新建数据库文件-var-named-wubinary-com-zone，并添加资源记录。"><a href="#4-新建数据库文件-var-named-wubinary-com-zone，并添加资源记录。" class="headerlink" title="4.新建数据库文件/var/named/wubinary.com.zone，并添加资源记录。"></a>4.新建数据库文件/var/named/wubinary.com.zone，并添加资源记录。</h3><blockquote>
<p>资源记录的格式：<br> name [ttl] IN RRtype Value<br> 资源记录名 有效时间 IN 类型 资源记录的值</p>
<p>SOA: 只能有一个，而且必须是第一个<br> name: 只能是区域名称，通常可以简写为@<br> value: 主DNS服务器的FQDN</p>
<p>NS: 可以有多条<br> name: 区域名称，通常可以简写为@<br> value: DNS服务器的FQDN(可以使用相对名称)</p>
<p>A: 只能定义在正向区域文件中<br> name: FQDN(可以使用相对名称)<br> value: IP</p>
<p>MX: 可以有多个<br> name: 区域名称，用于标识smtp服务器<br> value: 包含优先级和FQDN<br> 优先级：0-99，数字越小，级别越高；</p>
<p>CNAME:<br> name: FQDN<br> value: FQDN</p>
<p>PTR: IP –&gt; FQDN, 只能定义在反向区域数据文件中，反向区域名称为逆向网络地址加.in-addr.arpa.后缀组成<br> name: IP, 逆向的主机地址，主机地址反过来写加上.in-addr.arpa.<br> value: FQDN</p>
</blockquote>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/05.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$TTL为定义的宏，表示下面资源记录ttl的值都为600秒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;@符号可代表区域文件/etc/named.conf里面定义的区域名称，即：”wubinary.com.”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个区域的资源记录第一条必须是SOA，SOA后面接DNS服务器的域名和电子邮箱地址，此处电子邮箱地址里的@因为有特殊用途，所以此处要用点号代替。SOA后面小括号里的各值所代表的意义如下所示：</p>
<blockquote>
<p>@ IN SOA dns.wubinary.com dnsadmin.wubinary.com (<br> 2014031201 ;标识序列号，十进制数字，不能超过10位，通常使用日期<br> 2H ;刷新时间，即每隔多久到主服务器检查一次，此处为2小时<br> 4M ;重试时间，应该小于刷新时间，此处为4分钟<br> 1D ;过期时间，此处为1天<br> 2D ;主服务器挂后，从服务器至多工作的时间，此处为2天)</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;区域数据文件配置好后，可以使用命令named-checkzone检查语法错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令格式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">named-checkzone <span class="string">"zone_name"</span> zone_file_name</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/06.jpeg?raw=true)</p>
<h3 id="5-两个文件都配置好后，记得查看一下文件的所属组。因为bind程序的服务名称为named，bind默认是使用named组的身份操作文件，所以我们新建的文件所属组都要改为named，并且为了安全起见不能让别人有修改的权限，权限最好改为640。"><a href="#5-两个文件都配置好后，记得查看一下文件的所属组。因为bind程序的服务名称为named，bind默认是使用named组的身份操作文件，所以我们新建的文件所属组都要改为named，并且为了安全起见不能让别人有修改的权限，权限最好改为640。" class="headerlink" title="5.两个文件都配置好后，记得查看一下文件的所属组。因为bind程序的服务名称为named，bind默认是使用named组的身份操作文件，所以我们新建的文件所属组都要改为named，并且为了安全起见不能让别人有修改的权限，权限最好改为640。"></a>5.两个文件都配置好后，记得查看一下文件的所属组。因为bind程序的服务名称为named，bind默认是使用named组的身份操作文件，所以我们新建的文件所属组都要改为named，并且为了安全起见不能让别人有修改的权限，权限最好改为640。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/07.jpeg?raw=true)</p>
<h3 id="6-设置妥当当后我们就可以开启服务了。"><a href="#6-设置妥当当后我们就可以开启服务了。" class="headerlink" title="6.设置妥当当后我们就可以开启服务了。"></a>6.设置妥当当后我们就可以开启服务了。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/08.jpeg?raw=true)</p>
<h3 id="7-使用dig命令测试DNS。"><a href="#7-使用dig命令测试DNS。" class="headerlink" title="7.使用dig命令测试DNS。"></a>7.使用dig命令测试DNS。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令格式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dig [-t <span class="built_in">type</span>] [-x addr] [name] [@server]</div></pre></td></tr></table></figure>
<ul>
<li>-t: 指定资源类型，用于正解</li>
<li>-x: 指定IP地址，用于反解</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/09.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试成功！</p>
<h3 id="8-以上配置的是DNS服务器的正向解析，接着再配置一下反向解析。编辑配置文件-etc-named-rfc1912-zones，添加一个反解区域。"><a href="#8-以上配置的是DNS服务器的正向解析，接着再配置一下反向解析。编辑配置文件-etc-named-rfc1912-zones，添加一个反解区域。" class="headerlink" title="8.以上配置的是DNS服务器的正向解析，接着再配置一下反向解析。编辑配置文件/etc/named.rfc1912.zones，添加一个反解区域。"></a>8.以上配置的是DNS服务器的正向解析，接着再配置一下反向解析。编辑配置文件/etc/named.rfc1912.zones，添加一个反解区域。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/10.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为反向解是和正向解析相反的，所以配置文件192.168.0.70.zone直接可以复制wubinary.com.zone修改。反向解析数据文件里面只有SOA、NS、PTR资源记录，所有A记录都要改为PTR记录，名称为IP地址，IP地址可以写全也可以简写，如果写全则是IP地址反写加上.in-addr.arpa.例如：70.0.168.192.in-addr.arpa. PTR资源记录的值为域名。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/11.jpeg?raw=true)</p>
<h3 id="9-检查配置文件语法。"><a href="#9-检查配置文件语法。" class="headerlink" title="9.检查配置文件语法。"></a>9.检查配置文件语法。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/12.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改权限</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/13.jpeg?raw=true)</p>
<h3 id="10-重新载入配置文件，并测试反向解析。"><a href="#10-重新载入配置文件，并测试反向解析。" class="headerlink" title="10.重新载入配置文件，并测试反向解析。"></a>10.重新载入配置文件，并测试反向解析。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/14.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用dig -x 测试反向解析。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/15.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;反向解析配置成功！</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：通常在应用中，DNS的反向解析并不是很重要，可以不配置，当服务器中有域名作为邮件服务器时，此时可以配置反向解析，因为邮件中过滤垃圾邮件的技术通常是解析邮箱地址，如果IP地址不能反解成一个域名则视为垃圾邮件。</p>
<h2 id="三、使用bind架设辅助DNS服务器，实现主从数据同步"><a href="#三、使用bind架设辅助DNS服务器，实现主从数据同步" class="headerlink" title="三、使用bind架设辅助DNS服务器，实现主从数据同步"></a>三、使用bind架设辅助DNS服务器，实现主从数据同步</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DNS从服务器也叫辅服DNS服务器，如果网络上某个节点只有一台DNS服务器的话，首先服务器的抗压能力是有限的，当压力达到一定的程度，服务器就会宕机罢工，其次如果这台服务器出现了硬件故障那么服务器管理的区域的域名将无法访问。为了解决这些问题，最好的办法就是使用多个DNS服务器同时工作，并实现数据的同步，这样两台服务器就都可以实现域名解析操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主DNS服务器架设好后，辅助的DNS服务器的架设就相对简单多了。架设主从DNS服务器有两个前提条件，一是两台主机可以不一定处在同一网段，但是两台主机之间必须要实现网络通信；二，辅助DNS服务器必须要有主DNS服务器的授权，才可以正常操作。此时，我们以IP地址192.168.0.80作为我们辅助的DNS服务器的IP地址；</p>
<h3 id="1-设置IP地址；"><a href="#1-设置IP地址；" class="headerlink" title="1.设置IP地址；"></a>1.设置IP地址；</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/16.jpeg?raw=true)</p>
<h3 id="2-打开辅助DNS服务器的-etc-named-rfc1912-zones文件，添加两个区域记录，这两个记录是主DNS服务器配置文件里已经存在的记录，一个是正向解析记录，一个是反向解析记录。"><a href="#2-打开辅助DNS服务器的-etc-named-rfc1912-zones文件，添加两个区域记录，这两个记录是主DNS服务器配置文件里已经存在的记录，一个是正向解析记录，一个是反向解析记录。" class="headerlink" title="2.打开辅助DNS服务器的/etc/named.rfc1912.zones文件，添加两个区域记录，这两个记录是主DNS服务器配置文件里已经存在的记录，一个是正向解析记录，一个是反向解析记录。"></a>2.打开辅助DNS服务器的/etc/named.rfc1912.zones文件，添加两个区域记录，这两个记录是主DNS服务器配置文件里已经存在的记录，一个是正向解析记录，一个是反向解析记录。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/17.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;type: slave，表示此时DNS服务器为辅助DNS服务器，于是下面一行就要定义主DNS服务器的IP地址，辅助DNS服务器才知道去哪里同步数据。辅助DNS服务器的资源类型数据文件通常保存在slaves目录，只需定义一个名称，文件内容通常是自动生成。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置好后，直接开启DNS服务，然后再回到主DNS服务器上。</p>
<h3 id="3-修改主DNS服务器的数据文件，添加一条辅助DNS服务器记录，给辅助DNS服务器授权。"><a href="#3-修改主DNS服务器的数据文件，添加一条辅助DNS服务器记录，给辅助DNS服务器授权。" class="headerlink" title="3.修改主DNS服务器的数据文件，添加一条辅助DNS服务器记录，给辅助DNS服务器授权。"></a>3.修改主DNS服务器的数据文件，添加一条辅助DNS服务器记录，给辅助DNS服务器授权。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改正向解析文件/var/named/wubinary.com.zone。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/18.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加了一条NS记录，值为，ns2.wubinary.com.，对应的A记录也要增加一条，把IP地址指向对应的辅助DNS服务器的IP地址。修改完成后，记得要把序列号的值加1，用于通知辅助DNS服务器自动更新数据文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改反向解析文件/var/named/192.168.0.70.zone。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/19.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同样的也增加了两条记录，一条辅助DNS服务器的NS记录和对应的PTR记录。修改完成后记得所序列号的值加1，用于通知辅助DNS服务器自动更新数据文件。</p>
<h3 id="4-重新加载主DNS服务器的配置文件，这时再到回辅助DNS服务器，在-var-named-slaves-目录下会多了两个文件。"><a href="#4-重新加载主DNS服务器的配置文件，这时再到回辅助DNS服务器，在-var-named-slaves-目录下会多了两个文件。" class="headerlink" title="4.重新加载主DNS服务器的配置文件，这时再到回辅助DNS服务器，在/var/named/slaves/目录下会多了两个文件。"></a>4.重新加载主DNS服务器的配置文件，这时再到回辅助DNS服务器，在/var/named/slaves/目录下会多了两个文件。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/20.jpeg?raw=true   )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看文件内容，可以看到该文件和主DNS服务器上的文件内容是一样的。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/21.jpeg?raw=true)</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/22.jpeg?raw=true)</p>
<h3 id="5-测试辅助DNS服务器。"><a href="#5-测试辅助DNS服务器。" class="headerlink" title="5.测试辅助DNS服务器。"></a>5.测试辅助DNS服务器。</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/23.jpeg?raw=true)</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/24.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在辅助DNS服务器上正向解析和反向解析都能测试成功！</p>
<h2 id="四、主从同步数据的安全性"><a href="#四、主从同步数据的安全性" class="headerlink" title="四、主从同步数据的安全性"></a>四、主从同步数据的安全性</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DNS服务器的数据同步默认是没有限定主机的，也就是说，网络上只要有一台DNS服务器向你的DNS服务器请求数据，都能实现数据同步，那么这样就相当的不安全了。我们可以使用一个选项allow-transfer，指定可以同步数据的主机IP。主DNS服务器的数据可以给别的服务器同步，相对的，辅助DNS服务器的数据也是可以给其它辅助DNS服务器同步，于是，所有的主从DNS服务器都要设置该参数。</p>
<h3 id="1-指定可以从主DNS服务器上同步数据的主机。"><a href="#1-指定可以从主DNS服务器上同步数据的主机。" class="headerlink" title="1.指定可以从主DNS服务器上同步数据的主机。"></a>1.指定可以从主DNS服务器上同步数据的主机。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改/etc/named.rfc1912.zones文件：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/25.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在每块区域上添加参数allow-transfer，花括号内填写可以同步的主机IP，一般填写辅助DNS服务器的IP地址。可以使用dig命令测试，区域同步：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dig -t axfr ZONE_NAME @DNS_SERVCER_IP</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/26.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定IP可以同步数据。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/27.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;非指定IP不可以同步数据。</p>
<h3 id="2-指定可以从辅助DNS服务器上同步数据的主机。"><a href="#2-指定可以从辅助DNS服务器上同步数据的主机。" class="headerlink" title="2.指定可以从辅助DNS服务器上同步数据的主机。"></a>2.指定可以从辅助DNS服务器上同步数据的主机。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改/etc/named.rfc1912.zones文件：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/28.jpeg?raw=true)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们只有一台辅助DNS服务器，所以根本不会有主机从这台机器同步数据，所以我们设置成不允许任何人同步。</p>
<h2 id="五、测试DNS解析的其它命令"><a href="#五、测试DNS解析的其它命令" class="headerlink" title="五、测试DNS解析的其它命令"></a>五、测试DNS解析的其它命令</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 测试DNS解析的命令不只是dig可以实现，还有两个命令也可以实现相同的效果。</p>
<h3 id="1-host命令"><a href="#1-host命令" class="headerlink" title="1.host命令"></a>1.host命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;host命令格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"># host [-t type] &#123;name&#125; [server]</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/29.jpeg?raw=true)</p>
<h3 id="2-nslookup命令"><a href="#2-nslookup命令" class="headerlink" title="2.nslookup命令"></a>2.nslookup命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令很神奇，在windows的dos里面也可以使用：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">nslookup&gt;</div><div class="line">server DNS_SERVER_IP</div><div class="line"><span class="built_in">set</span> q=TYPE</div><div class="line">&#123;name&#125;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DNS%E6%9C%8D%E5%8A%A1%E5%99%A8(%E4%BA%8C" alt=""></figure>%EF%BC%9A%E4%BD%BF%E7%94%A8bind%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8EDNS%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5/30.jpeg?raw=true)</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/40">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
